This is my file.py
:
import os
os.chdir('/home/python3.10')
print(os.getcwd())
print(os.system('pwd; ls'))
import template
print(template.text)
I have a virtual environment in /home/python3.10
.
If I run file like this:
/home/python3.10/venv/bin/python3.10 /home/another_dir/file.py
I get this error:
/home/python3.10
/home/python3.10
__pycache__ file1.txt requirements.txt template.py test.py venv
0
Traceback (most recent call last):
File "/home/another_dir/file.py", line 5, in <module>
import template
ModuleNotFoundError: No module named 'template'
But if I run /home/python3.10/venv/bin/python3.10
in the shell, then I run all of above lines, it works:
Python 3.10.13 (main, Aug 25 2023, 13:20:03) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>>
>>> os.chdir('/home/python3.10')
>>> print(os.getcwd())
/home/python3.10
>>> print(os.system('pwd; ls'))
/home/python3.10
__pycache__ file1.txt requirements.txt template.py test.py venv
0
>>> import template
>>> print(template.text)
{text}
What's the reason I have this error? I see the getcwd()
is the correct directory, but I'm not sure why it cannot see template.py
to import as module.
template.py
gets installed to your virtual environment'slib/site-packages
directory, and your script gets install to your virtual environment'sbin
directory.PATH=/root
when running behind the crontab. So, I need to tell where the path sotemplate.py
is. Do you have any idea for this?