I have a file system as follows:
/
- website/
- - server.py
- common.py
When in /, i try to run: python website/server.py but it shows an error when attempting to from common import my_func saying:
ImportError: No module named common
Is there a way to denote a file as a module? The issue I am running into is that while working in PyCharm it understands the python files correctly and functions as designed, but when running them in the command prompt in a VM it doesnt understand. I was running the python from / as you can see, thinking it would use that as the scope, but it seems like that isnt working.
I was not given any more information from the debugger or logs. What am I doing wrong?
Edit I tried doing the following as per Joao's request. python ./website/server.py and returned the following line still, which is the same error received above.
Traceback (most recent call last):
File "./website/server.py", line 3, in <module>
from common import my_func
Import Error: No module named common
3 Answers 3
You're trying to import from the parent folder.
Add the following code before from common import my_func will do the trick.
import sys
#appending parent directory into the path
sys.path.append('..')
1 Comment
/ but instead /website/In PyCharm PYTHONPATH is set automatically. Assuming you're running server.py from the directory containing common.py, you could define PYTHONPATH to that directory an run.
$ python website/server.py
Traceback (most recent call last):
File "website/server.py", line 1, in <module>
from common import my_func
ImportError: No module named common
$ PYTHONPATH=`pwd` python website/server.py
$
The key here is to make sure the packages you need are in the paths that Python will search when running your code.
6 Comments
'PYTHONPATH' is not recognized as an internal or external command, operable program or batch file.Try
python ./website/server.py
2 Comments
common.py is not in the same directory as server.py.