0

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
John Percival Hackworth
11.5k2 gold badges31 silver badges38 bronze badges
asked Jul 5, 2017 at 17:48

3 Answers 3

1

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('..')
answered Jul 5, 2017 at 17:55
Sign up to request clarification or add additional context in comments.

1 Comment

This is a solution. This will only work though with the following understanding: Your current directory is not / but instead /website/
0

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.

answered Jul 5, 2017 at 17:57

6 Comments

Is this for windows? it says that when doing that command, 'PYTHONPATH' is not recognized as an internal or external command, operable program or batch file.
No, this is for Unix using bash. If you're running on Windows, take a look at stackoverflow.com/questions/3701646/…
I think that assumed that the path is a global set of common modules. Mine is actually just a list common functionality for a project, so adding it as such would not make sense and expose too much outside of the project.
Probably exposing the directory globally doesn't make much sense, but the fact remains that in order to use the directory structure you have for development you need to have PYTHONPATH set appropriately. For an example approach to temporarily setting a variable see: stackoverflow.com/questions/2901404/…
In my advanced variables, PYTHONPATH does not exist. This is neither in globals or local env variables. This MIGHT be something created from within python execution, but i dont really see this in documentation. This seems like it is more so a suggestion to put all python in that variable and then put it in the PATH variable. Understandably so, but making something global isnt the answer, and likely would need to be accomplished project only. Probably.something similar to local run time of the project leveraging the answer target script. Still seems to be not the best solution either.
|
-1

Try

python ./website/server.py
Baum mit Augen
50.4k25 gold badges153 silver badges187 bronze badges
answered Jul 5, 2017 at 17:50

2 Comments

Ill update the question for formatting, but i still get that error.
That's not the issue, the problem is common.py is not in the same directory as server.py.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.