I am trying to deploy my project to a server and run it there. When I try to start a script from command line it shows errors when importing scripts that are in parrent directories.
I made the project (python 2.7.10) using PyCharm and it is spread out into multiple directories. The folders look something like this:
project/dir/subdir/main_dir/script1.py
from dir.subdir.other_dir.script2 import * //gives error here
project/dir/subdir/other_dir/script2.py
def my_function():
//do something
I run the script by going to the main_dir and running: python script1.py
If you are running your script from the main_dir
, that means when running your Python command, your relative reference is main_dir
. So your imports are with respect to main_dir
being your root.
This means if we take your script1 for example, your import should look like this:
from other_dir.script2 import *
Chances are your PyCharm project root is actually set to run from
project/
Which is why your references work within PyCharm.
What I suggest you do is, if your server is supposed to run within main_dir
then you should re-configure PyCharm so that its execution root is the same in order to remove this confusion.
An alternative solution to this problem in my case was to add a main.py script in the root of the python project which triggers the program.
project/__main__.py:
from dir.subdir.other_dir.script2 import * //doesn't give errors
This means that when calling the program from the terminal the workspace would be correct and every inclusion of script would have the folders mapped correctly (from the root).
project/dir/subdir/main_dir/script1.py:
from dir.subdir.other_dir.script2 import * //also doesn't give errors
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With