ImportError on console but not in PyCharm - python

I have the folowing dir structure
|population_model
--|__init__.py
--|run.py
inside __init__.py I have the following:
def my_func():
...
return
on run.py I have
from population_model import my_func
When I run the project from inside PyCharmthe code runs beautifully.
But when running from Terminal:
my/path/to/population_model/python run.py
I get ImportError: No module named population_model
What might be the cause?

Most likely PyCharm's Run configuration is setting the Working Directory to your project, where it can find population_model. In the terminal, cd to the directory first.

Related

Python ModuleNotFoundError despite __init.py__ and correct PYTHONPATH on Linux

I'm trying to get a Python script running on my Ubuntu server. I have the following directory structure:
/home/pythontest
|_ __init__.py
|_ main.py
|_ module_a.py
inside module_a.py:
def print_a():
print('a')
inside main.py:
from pythontest.module_a import print_a
def execute():
print_a()
execute()
When I run main.py in PyCharm on my Windows machine, it prints a as expected, on my Linux machine, when I call python3 main.py I get a
Traceback (most recent call last):
File "main.py", line 1, in <module>
from pythontest.module_a import print_a
ModuleNotFoundError: No module named 'pythontest'
The __init__.py exists (and is completely empty) and I have added the directory /home/pythontest to the PYTHONPATH with the following command:
export PYTHONPATH="${PYTHONPATH}:/home/pythontest"
(testing this with echo $PYTHONPATHalso yields the correct path)
Additional Notes:
- The python3 version on my machine is Python 3.6.9
- My Server runs Ubuntu 18.04
- All those files are written in PyCharm on Windows and copied over via SSH
You're importing pythontest(.module_a), which is located in /home. That's what you're supposed to add to PYTHONPATH:
export PYTHONPATH=${PYTHONPATH}:/home
More details on [Python.Docs]: The import system.
Or you could not reference the package name from within it (consider relative imports):
from .module_a import print_a
Might also want to check [SO]: How PyCharm imports differently than system command prompt (Windows) (#CristiFati's answer), to see why does it work from PyCharm.
You need to change your import in main.py to:
from module_a import print_a
since module_a is a module that exists in the path that you exported.

Python3 relative imports failing in package

I've been reading tons of questions related to this matter but none of the has help me so far. I'm currently using the Python click library to execute scripts as commands.
The current command that I'm trying to execute is placed inside a Python Package which has a __main__.py file, like the parent dir has. The current project structure is the following one.
/myproject
/foo_one
__init__.py
foo_one.py
/foo_two
__init__.py
foo_two.py
/foo_three
__init__.py
foo_three.py
/foo_four
__init__.py
foo_four.py
/foo_five
__init__.py
foo_five.py
/foo_six
__init__.py
foo_six.py
__init__.py
__main__.py
foo_seven.py
Whenever I try to run the __main__.py script located in the project folder, the following error comes up.
ModuleNotFoundError: No module named '__main__.foo_two'; '__main__' is not a package
However, if I try to execute that same script from a folder above with the -m option like this python3 myproject -m, the following is shown up.
ImportError: attempted relative import with no known parent package
The __main__.py has 2 imports like this... The __init__.py is empty.
from .foo_two.foo_two import AClass, AnotherClass, OtherClass
from .foo_three.foo_three import AnotherClassMore
UPDATE: Correcting the syntax error in a previous command, while calling python -m myproject gives me a ModuleNotFoundError because of a module that isn't my responsibility, which is basically a library that is used in the project.
Hopefully, this will be of value to someone out there - I went through half a dozen stackoverflow posts trying to figure out relative imports similar to whats posted above here. I set up everything as suggested but I was still hitting ModuleNotFoundError: No module named 'my_module_name'
Since I was just developing locally and playing around, I hadn't created/run a setup.py file. I also hadn't apparently set my PYTHONPATH.
I realized that when I ran my code as I had been when the tests were in the same directory as the module, I couldn't find my module:
$ python3 test/my_module/module_test.py 2.4.0
Traceback (most recent call last):
File "test/my_module/module_test.py", line 6, in <module>
from my_module.module import *
ModuleNotFoundError: No module named 'my_module'
However, when I explicitly specified the path things started to work:
$ PYTHONPATH=. python3 test/my_module/module_test.py 2.4.0
...........
----------------------------------------------------------------------
Ran 11 tests in 0.001s
OK
So, in the event that anyone has tried a few suggestions, believes their code is structured correctly and still finds themselves in a similar situation as myself try either of the following if you don't just add your export the current directory to your PYTHONPATH:
Run your code and explicitly include the path like so:
$ PYTHONPATH=. python3 test/my_module/module_test.py
To avoid calling PYTHONPATH=., create a setup.py file with contents like the following and run python setup.py development to add packages to the path:
# setup.py
from setuptools import setup, find_packages
setup(
name='sample',
packages=find_packages()
)
The correct syntax would be
python -m myproject
This should execute __main__ in the top-level package.
You need to have __init__.py in each sub folder with python code to tell the interpreter to treat the folder as a module
/myproject
/foo_one
__init__.py # add this
foo_one.py
/foo_two
__init__.py # add this
foo_two.py
/foo_three
__init__.py # add this
foo_three.py
/foo_four
__init__.py # add this
foo_four.py
/foo_five
__init__.py # add this
foo_five.py
/foo_six
__init__.py # add this
foo_six.py
__init__.py
__main__.py
foo_seven.py
the __init__.py is telling the interpreter to treat sub folders as python modules / packages and you should be able to import
The __init__.py file can be empty but needs to be present in the sub folders to be able to import that module / package

Python "module not found" error when importing from folder?

I have the common problem of the "module not found" error when trying to import a file within a folder in my project directory as a package. I've tried several solutions from Stackoverflow answers, but none are working for me. Here's what's going on, and what I've tried:
I'm working in a conda environment devenv on a Flask project, using PyCharm, and have a project directory like this:
/some/path/project_root/
migrations/
static/
templates/
reporting/
__init__.py
code.py
tests.py
Inside the tests.py file there are import statements to import code.py as a module:
from .code import my_function
However, when I run (devenv) me#comp:project_root$ > python reporting/tests.py
I get the error: ModuleNotFoundError: No module named '__main__.code'; '__main__' is not a package
I tried appending the project directory path to $PYTHONPATH, and echo $PYTHONPATH returns /some/path/project_root/
What do I need to configure to get this to work properly? Also, whatever settings I need to change, can I make those settings specific to the development environment I'm using?
Change from .code import my_function to from code import my_function. The top level of a package is defined by the highest folder with an __init__.py file. So the top level of your project is the reporting folder and code.py does not need to be a relative import. Best to either avoid relative imports or get an editor like PyCharm that will take care of it for you!

Python not finding modules that I have added to PYTHONPATH?

My file structure looks like this:
main
--src_module
----__init__.py
----utils.py
----tests
------test.py
PYTHONPATH looks like: '/Users/myName/main/src_module'
test.py has the line:
from src_module.utils import something
but when I try to run python test.py, I get the following error:
ImportError: No module named src_module.utils
I was under the impression that adding the src_module folder to PYTHONPATH would fix this problem, but clearly it hasn't. Can someone explain to me why I'm still experiencing these import errors?
You can access the utils.py file from tests directory with this structure.
main
--src_module
----__init__.py
----utils.py
--tests
----__init__.py
----test.py
Also you can use a test runner like nose2 to run your tests with command nose2 or if you would like to run the test.py file by itself then you can run via
python -m tests.test

Python : Having issue to Import a function from a file in a different folder

in my python project, I have 2 folders:
folder_A
__init__.py
a.py
b.py
c.py
folder_B
__init__.py
main.py
With in main.py, I am using this command:
from folder_A.a import function1
When running the program I get:
ImportError: No module named 'folder_A'
What am I doing wrong?
Upon execution, the current folder is added to the python path. However if you are executing
~/myproject/$ python folder_B/main.py
Then the current path resolves inside folder_B, so folder_A is not in the python path.
You can execute the main module from the upper project folder:
~/myproject/$ python -m folder_B.main
Otherwise, you can set the PYTHONPATH env var
~/myproject/folder_B/$ PYTHONPATH=".." python main.py

Categories

Resources