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,395
  • 2
  • 29
  • 38
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

3 Answers3

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('..')
  • This is a solution. This will only work though with the following understanding: Your current directory is not `/` but instead `/website/` – Fallenreaper Jul 05 '17 at 18:03
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.

John Percival Hackworth
  • 11,395
  • 2
  • 29
  • 38
  • 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.` – Fallenreaper Jul 05 '17 at 18:02
  • No, this is for Unix using bash. If you're running on Windows, take a look at https://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-7 – John Percival Hackworth Jul 05 '17 at 18:05
  • 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. – Fallenreaper Jul 05 '17 at 18:48
  • 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: https://stackoverflow.com/questions/2901404/temporary-pythonpath-in-windows – John Percival Hackworth Jul 05 '17 at 19:10
  • 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. – Fallenreaper Jul 06 '17 at 13:55
  • Your best option would be to rearrange your directory structure so common.py is in the website directory and convert the website directory into a package. Failing that, review the Python documentation with reference to the PYTHONPATH variable. https://docs.python.org/3/using/cmdline.html?highlight=pythonpath#environment-variables or https://docs.python.org/2/using/cmdline.html?highlight=pythonpath#environment-variables . – John Percival Hackworth Jul 06 '17 at 16:48
-1

Try

python ./website/server.py
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182