I would understand this behaviour.
In Pycharm I can successfully run (green play button) this main.py which includes src.v0.Det.py
src
|
+--v0
| |
| +--Det.py (class det)
| __init__.py
|
+-- main.py
__init__.py
main.py
------------
from src.v0.Det import det
....
But when I run on unix using: python main.py I get "No module named src.v0.Det".
I can obviously remove "src" and it works but I don't want to change the file. How can I run this file like in Pycharm?
Riccardo
export PYTHONPATH="${PYTHONPATH}:<.....path.to.src.without.src....>"
Related
Image of my file and Folder layout
Trying to import modules from grandparent files. Searching told me to use init.py files. I actually fixed it using them, and my file structure was:
main.py
app.py
...
Tests |
| - __init__.py
| - testrunner.py
| ...
In this example, testrunner would import a module from app.py.
This worked, but recently added more testfiles and wanted to organize more. So now my file structure is:
main.py
app.py
...
Tests |
| - __init__.py
| - Sprint 1
| - __init__.py
| - testrunner.py
| ...
Now it doesn't work anymore. I can move them back into Tests and the file runs fine again, but not in Sprint 1. There I still get the "E ModuleNotFoundError: No module named 'App'" error
I tried deleting and recreating the __init__.py files, reopening the project, adding (import sys
sys.path.append('..')) to testrunner.py. None of it worked, though I can't for the life of me figure out why adding the __init__.py files didn't work now for some reason.
I moved my tests to a separate subfolder in the project, and now testing my classes does not work anymore.
|-- project
| |main.py
| |-- lib
| | |__init__.py
| | |myclass.py
| |-- tests
| | |__init__.py
| | |test_myclass.py
Both init files are empty.
but when I run the test (i'm in the tests folder and typing python -m unittest) I get the following error:
ModuleNotFoundError: No module named 'lib'
at the line from lib.myclass import Myclass
I also saw that one could use sys and os to add the parent folder to the path, but each time that I do it, the code that adds it is automatically going under all the imports in vscode (I guess due to some automatic formatting?) and therefore is not ran on time.
That's because once you are in tests folder your working directory dir is:
.\
.\test_mycalss.py
You should run tests from project so your working directory will cover whole tree:
.\
.\main.py
.\lib\
and so on
Your project cannot view lib.myclass because in folder tests there is no folder lib.
I am having trouble importing a module from within another module. I understand that that sentence can be confusing so my question and situation is exactly like the one suggested here: Python relative-import script two levels up
So lets say my directory structure is like so:
main_package
|
| __init__.py
| folder_1
| | __init__.py
| | folder_2
| | | __init__.py
| | | script_a.py
| | | script_b.py
|
| folder_3
| | __init__.py
| | script_c.py
And I want to access code in script_b.py as well as code from script_c.py from script_a.py.
I have also followed exactly what the answer suggested with absolute imports.
I included the following lines of code in script_a.py:
from main_package.folder_3 import script_c
from main_package.folder1.folder2 import script_b
When I run script_a.py, I get the following error:
ModuleNotFoundError: No module named 'main_package'
What am I doing wrong here?
This is because python doesn't know where to find main_package in script_a.py.
There are a couple of ways to expose main_package to python:
run script_a.py from main_package's parent directory (say packages). Python will look for it in the current directory (packages), which contains main_package:
python main_package/folder_1/folder_2/script_a.py
add main_package's parent directory (packages) to your PYTHONPATH:
export PYTHONPATH="$PYTHONPATH:/path/to/packages"; python script_a.py
add main_package's parent directory (packages) to sys.path in script_a.py
In your script_a.py, add the following at the top:
import sys
sys.path.append('/path/to/packages')
Hello Python Programmers
I'm getting a weird module import error during unittest. Not able to find the root cause.
Here is how my directory structure looks like
Main_folder
|
|
Module_x
| ABC.py
|
|
Module_y
| DEF.py
|
|
test
| unit_tests
| test_ABC.py
In test_ABC.py I'm importing the following
from Module_x import ABC
I get the error as "No module name Module_x"
I've created __init__.py file at each folder
I don't get any error if I use the same line from DEF.py
Please help if you're aware about why I am getting this issue?
I am using Python 3.5 Anaconda Distribution
Thanks
This is not a weird problem but a common one.
You can add your root into PYTHONPATH to solve this problem: PYTHONPATH=/path/to/project_root python test_ABC.py or something else similar.
I have a python 2.7 project which I have structured as below:
project
|
|____src
| |
| |__pkg
| |
| |__ __init__.py
|
|____test
|
|__test_pkg
| |
| |__ __init__.py
|
|__helpers
| |
| |__ __init__.py
|
|__ __init__.py
I am setting the src folder to the PYTHONPATH, so importing works nicely in the packages inside src. I am using eclipse, pylint inside eclipse and nosetests in eclipse as well as via bash and in a make file (for project). So I have to satisfy lets say every stakeholder!
The problem is importing some code from the helpers package in test. Weirdly enough, my test is also a python package with __init__.py containing some top level setUp and tearDown method for all tests. So when I try this:
import helpers
from helpers.blaaa import Blaaa
in some module inside test_pkg, all my stakeholders are not satisfied. I get the ImportError: No module named ... and pylint also complains about not finding it. I can live with pylint complaining in test folders but nosetests is also dying if I run it in the project directory and test directory. I would prefer not to do relative imports with dot (.).
The problem is that you can not escape the current directory by importing from ..helpers.
But if you start your test code inside the test directory with
python3 -m test_pkg.foo
the current directory will be the test directory and importing helpers will work. On the minus side that means you have to import from . inside test_pkg.