Reputation: 16147
Here's my Python folder structure
-project
----src
------model
--------order.py
------hello-world.py
Under src
, I have a folder named model
, which has a Python file called order.py
, whose contents follow:
class SellOrder(object):
def __init__(self,genericName,brandName):
self.genericName = genericName
self.brandName = brandName
Next, my hello-world.py
is inside the src
folder, one level above order.py
:
import model.order.SellOrder
order = SellOrder("Test","Test")
print order.brandName
Whenever I run python hello-world.py
it results in the error
Traceback (most recent call last):
File "hello-world.py", line 1, in <module>
import model.order.SellOrder
ImportError: No module named model.order.SellOrder
Is there anything I missed?
Upvotes: 119
Views: 648867
Reputation: 533
A lot of answers. But I have just experienced this. I hope this could help someone:
On the top of the file you import
from the-folder-you-import import yourfilename
example structure
src
incrementNum.py
tests
test_unitest.py
Upvotes: 0
Reputation: 81
It's also important to not that sometimes the solution is a lot simpler than you think. None of the previous solutions worked for me and after some time I came to realise that it was just that I had a leading space in the filename for my 'init.py' file.
I removed the leading space and the crisis was averted.
Upvotes: 1
Reputation: 29
I have write in PowerShell,
$env:PYTHONPATH = "C:\Users\adolfo\Desktop\borrar"
with the route of the root folder of the project.
Upvotes: 0
Reputation: 1
I solved it by deleting previous Python 2 and only using Python 3 which is working fine on Windows 10.
Upvotes: -2
Reputation: 47
If you are using Visual Studio Code, changing the interpreter of my IDE worked for me, and here is a quick snapshot:
I install my packages through pip3, and it appears to be like my Homebrew handles all of the packages I installed previously, so that's the tweak I had to make!!
Upvotes: 0
Reputation: 191
Just add your project root directory to an environment variable: PYTHONPATH.
So for the below project structure, just add the Rootdir path (for example: add E:\Projects\Rootdir) in PYTHONPATH.
Rootdir
└── pkg2
├── b.py
├── c.py
└── pkg2
├── b.py
├── c.py
Upvotes: 3
Reputation: 170
If you are using Python 3, then try the below command. I was facing a similar issue , this fixed my problem
pip3 install <module name>
Upvotes: -2
Reputation: 91
I only use Python as a secondary language and probably made a newbie error. I had a similar problem and my error was calling:
import requests
I got the error
ModuleNotFoundError: No module named 'requests.adapters'; 'requests' is not a package
It turns out the file I created in the same folder named "requests.py" made a conflict. Renaming the file made it work again.
Upvotes: 2
Reputation: 3356
If it's your root module, just add it to PYTHONPATH
(PyCharm usually does that):
export PYTHONPATH=$PYTHONPATH:<root module path>
For Docker:
ENV PYTHONPATH="${PYTHONPATH}:<root module path in container>"
Upvotes: 32
Reputation: 7
You need to import the function, so the program knows what that is. Here is an example:
import os
import pyttsx3
I had the same problem first, but then I imported the function and it worked, so I would really recommend to try it.
Upvotes: -8
Reputation: 485
I had the same error.
For those who run Python scripts on different servers, please check if the Python interpreter path is correctly specified in the shebang. For me, on each server, it was located in different directories.
Upvotes: 1
Reputation: 11
After trying to add the path using:
pip show
on a command prompt and using
sys.path.insert(0, "/home/myname/pythonfiles")
it didn't work. I also got an SSL error when trying to install the module again using Conda this time instead of pip.
I simply copied the module that wasn't found from the path. Mine was in
C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages
so I copied it to 'C:\Users\user\Anaconda3\Lib\site-packages'
.
Upvotes: 1
Reputation: 390
You can check to see if a module is installed for Python by running:
pip uninstall moduleName
If it is installed, it will ask you if you want to delete it or not. My issue was that it was installed for Python, but not for Python 3. To check to see if a module is installed for Python 3, run:
python3 -m pip uninstall moduleName
After doing this, if you find that a module is not installed for one or both versions, use these two commands to install the module.
Upvotes: 1
Reputation: 4588
All modules in Python have to have a certain directory structure. You can find details here.
Create an empty file called __init__.py
under the model
directory, such that your directory structure would look something like that:
.
└── project
└── src
├── hello-world.py
└── model
├── __init__.py
└── order.py
Also in your hello-world.py
file change the import statement to the following:
from model.order import SellOrder
That should fix it
P.S.: If you are placing your model
directory in some other location (not in the same directory branch), you will have to modify the python path using sys.path
.
Upvotes: 80
Reputation: 568
You need a file named __init__.py
(two underscores on each side) in every folder in the hierarchy, so one in src/
and one in model/
.
This is what Python looks for to know that it should access a particular folder. The files are meant to contain initialization instructions, but even if you create them empty, this will solve it.
Upvotes: 11
Reputation: 11
Create a setup.py file.
from setuptools import setup, find_packages
setup(name= 'any_name', version= '1.0', packages=find_packages())
then in cmd give:
pip install -e .
Upvotes: 0
Reputation: 6534
Another solution depends on where you are running this code from.
If you try running python hello-world.py
(from the src directory), you would have to do the following two things for this to work:
from model.order import SellOrder
PYTHONPATH
, value .
Upvotes: 0
Reputation: 26
It's easier if you use this code
python3 -m module.sub_module
For example:
python3 -m entrypoint.settings
Upvotes: 4