I've made a separate directory for my django modules. I've added this directory to my PATH variable. I've also created a new PYTHONPATH variable, since it wasn't there. I've added modules to settings.py. But when im trying to run manage.py syncdb for the new module it still says
Error: No module named my_module
Why, oh why?
EDIT: I didn't created the app with manage.py startapp, but manually created the files. Can this cause the problem?
PATH tells your shell where to find executables; it has nothing to do with Python. PYTHONPATH is a list of directories to search for Python modules. It should be edited to include the directory with my_module.
Hard to tell you what the issue is with only that output, however this should solve the problem:
Inside bar.py or bar/__init__.py
import os,sys
sys.path.append(os.path.dirname(__file__))
Now, in other files you can import bar
Related
I've made a separate directory for my django modules. I've added this directory to my PATH variable. I've also created a new PYTHONPATH variable, since it wasn't there. I've added modules to settings.py. But when im trying to run manage.py syncdb for the new module it still says
Error: No module named my_module
Why, oh why?
EDIT: I didn't created the app with manage.py startapp, but manually created the files. Can this cause the problem?
PATH tells your shell where to find executables; it has nothing to do with Python. PYTHONPATH is a list of directories to search for Python modules. It should be edited to include the directory with my_module.
Hard to tell you what the issue is with only that output, however this should solve the problem:
Inside bar.py or bar/__init__.py
import os,sys
sys.path.append(os.path.dirname(__file__))
Now, in other files you can import bar
I have been trying to add several paths to environment variable in windows-10 but still repeatedly getting Module not found error?
Mycurrent path to envirionment variables
when I try to run my training script I get Module not found error.
error while executing
Please help me to add correct path to envirionment so that I would not get "module not found error" ever again.
you can give the path to python script like this
<python installation path>\Scripts\
you can check the python installation path in windows by entering this command in cmd.
where python
It looks like a pretty complex directory structure. I wonder whether the fact you have a subdirectory with the same name (regression_model) as its parent is causing problems?
In general what you need to make the following happen:
the module you want to import should be a directory with an __init__.py file inside it
the parent directory of the module directory should be in the pythonpath
You could try renaming one of the directories and simplifying what's in the environment variable to match the above.
If that doesn't work
import sys
print(sys.path)
to check what the script thinks is in the pythonpath.
(you can even try using sys.path.append within the script to put the right thing into the path if all else fails! Might help with debugging.)
I recently figured out how to import modules for unittesting in python. As a solution to this, I use:
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from Dev.test import someclass
This works fine while running in PyCharm and I get the expected output. However, when I run from terminal I run into an error:
ImportError: No module named Dev.test
I have the init files where they are supposed to be but I'm lost as to why this is working in PyCharm but not from the terminal. I have not changed my path or anything in PyCharm as this code is supposed to be able to run with minimal modifications on other machines. Any idea as to why this is happening and what I might be able to do to fix it?
My folder structure is as follows
-Current
-Dev
-__init__.py
-test
- __init__.py
-someclass.py
-Tests
-__init__.py
-someunittest.py
I have tried running someunittest from the main folder as well as with a complete path but it only works in PyCharm
sys.path.append(os.getcwd()[:os.getcwd().index('Dev')])
I added this to my imports and it seems to have solved the problem. However, this doesn't seem like it would be the right way to do it; it will do for now.
When running a script from within PyCharm, it runs it in an environment with PYTHONPATH set to the list of all the folders that are marked "Sources Root" (with a blue folder icon) in the project explorer.
Outside of PyCharm, PYTHONPATH is not normally set. The first entry in sys.path refers to the current working directory where the script was run from. As long as you run your script with your terminal's working directory as the folder containing Dev, it should be able to find the Dev.test module, regardless of the extra entry added to sys.path.
Once you get the working directory correct, you should be able to remove the sys.path hack.
What #codewarrior has said about the PyCharm setting its own PYTHONPATH is correct. But sys.path didn't have my current working directory. So to get around this problem, I updated my PYTHONPATH (or you can edit sys.path).
Setting PYTHONPATH
export PYTHONPATH=$PYTHONPATH:`pwd` (OR your project root directory)
Updating sys.path
import sys
sys.path.insert(0,'<project directory>') OR
sys.path.append('<project directory>')
You can use insert/append based on the order in which you want your project to be searched.
HTH.
Pycharm uses a virtual environment. When you try run your program in your terminal this enviroment isn't active.
You need to build or upload your enviroment Pycharm with your libraries.
cd to the directory project and write in terminal:
source venv/bin/activate
I too have had this issue - and the PYTHONPATH setting set by PyCharm did seem to be the issue.
My alternative (as I was nearly finished writing the code) was to generate a setup.py - and install the classes/structure in my local virtual Python environment.
I would recommend trying out $ pip install . in your source directory. This will install your own packages for your project.
To add to similar answers here, PyCharm is doing some extra config for you before running your script. If adding your sources root to PYTHONPATH doesn't work then examine your run configuration in PyCharm for the script in question, there will likely be some more behind the scenes magic at play.
I had similar problem. I think the problem is that Pycharm modifies PYTHONPATH so before running your script:
cd to the file where python file resides
run export PYTHONPATH=.
run the script
You can also create "main" python file where you set the python path and then call the other modules
This problem has been driving me nuts. I am trying to import a class from a file in the same directory. PyCharm is giving me the "Unresolved reference" error. MyClass is defined in file.py.
I have found these questions:
Unresolved reference issue in PyCharm
Pycharm: "unresolved reference" error on the IDE when opening a working project
PyCharm shows unresolved references error for valid code
Unresolved reference when importing from sibling sub-package with
I have the following project structure:
I have marked src as the sources root...
I have set the "Add source roots to PYTHONPATH":
I have tried File -> Invalidate Caches / Restart.. (I even restarted the computer).
If I try to run it, I get the following error in the console: ImportError: cannot import name 'MyClass'
The interpreter is a virtualenv on Python 3.4 on Ubuntu x64 14.04.
If I install and import any 3rd party packages, they work fine.
If I try echo $PYTHONPATH in the terminal it returns nothing (same with env | grep PYTHONPATH. I have the appropriate virtualenv active when I try these.
Any clues?
If MyClass is defined in pack/file.py, you need to import it as:
from pack.file import MyClass
Note that using names of Python built-in types (such as file) for your own modules is a bad idea.
If you are using python version 3 try this
from .pack import myclass
This worked for me
The following steps solved my issues:
All directories required at least a blank __init__.py file
Mark all directories as source roots (per previous poster instructions)
Yes, if you are using python 3 you should add something like this:
from .pack import MyClass
It will work
I had the same issue when I tried to import a new class, however I could successfully import functions from a file in the same directory. I still dont understand why I could not import my class but thought I would share the information for other users.
#kaylebs response worked for me. However I then added the src directory to the list of source directories, first link in #lulian 's question and could remove the '.' from my file name.
There are several reasons why this could be happening. Below are several steps that fixes the majority of those cases:
.idea caching issue
Some .idea issue causing the IDE to show error while the code still runs correctly. Solution:
close the project and quick PyCharm
delete the .idea folder where the project is. note that it is a hidden folder and you might not be aware of its existence in your project directory.
start PyCharm and recreate the project
imports relative not to project folder
Relative imports while code root folder is not the same as the project folder. Solution:
Find the folder that relative imports require in the project explorer
right click and mark it as "Source Root"
Editor not marking init.py as Python
Which is the most illusive of all the cases. Here, for some reason, PyCharm considers all __init__.py files not to be python files, and thus ignores them during code analysis. To fix this:
Open PyCharm settings
Navigate to Editor -> File Types
Find Python and add __init__.py to the list of python files or find Text and delete __init__.py from the list of text files
I just delete the copied the code and delete the file and again create the same, that time it will work
I want to rename a project which I created with:
django-admin.py startproject
But after renaming the folder and all the references inside my project, I still can't get it to start. It says myproject.settings is not in the pythonpath. Since the old project name is neither in the pythonpath i figure that django must keep these names and paths somewhere else. Where does it store this information ?
I know I could just add the path to sys.path while execution, but i want to fix this completely.
If i run:
python manage.py runserver
I get:
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named settings
The name for 'mysite' is already the correct one and corrosponds with the folder name. But still it can't find it.
Any ideas?
PS: I'm running debian.
It doesn't store the project path anywhere. Everything is calculated relative to the path you specified for the settings module.
If you renamed your project folder and it's still trying to load the old settings, it's possible that you still have the old settings file configured somewhere, e.g. in the environment DJANGO_SETTINGS_MODULE is still pointing to oldproject.settings or your WSGI server is still configured to load oldproject.settings. Also check that you don't have any package weirdness in your interpreter's site-packages.
i'm sorry, but the solution was more simple in my case:
one of the apps had the same name i wanted to give to the project. this resulted in the described error message !
sorry for the trouble.