Reputation: 1086
I've created the following package tree:
/main_package
/child_package
version.py
where version.py contains a single string variable (VERSION)
Inside my script in child package I'm importing version.py by the following line:
from main_package.version import VERSION
While I'm running the code from PyCharm everything works great, however when I'm running the code via the command line I'm getting the following error message:
C:\Users\usr\PycharmProjects\project\main_package\child_package>python script.py
Traceback (most recent call last):
File "script.py", line 2, in <module>
from main_package.version import VERSION
ModuleNotFoundError: No module named 'main_package'
I've found in the internet that I might need to add my package to the python path, however it doesn't seems to work for me
Upvotes: 2
Views: 2372
Reputation: 10609
The pythonic way is to have a setup.py
file to install your project in the system (check python Minimal Structure):
from setuptools import setup
setup(name='main_package',
version='0.1',
description='main package',
license='MIT',
packages=['main_package'],
zip_safe=False)
Then you install it as follow:
python setup.py install
for global installationOR
python setup.py develop
for local installation in editable modeUpvotes: 1
Reputation: 1318
PyCharm sets the Python Path at the root of the project (by default). To mimic this in a quick'n'dirty fashion, you just need to do this once in your shell session before invoking python whatever
:
set PYTHONPATH=C:\Users\usr\PycharmProjects\project
Upvotes: 5