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 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.)
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
Disclaimer: I am new to python and django but have Drupal programming experience. I'm using Windows 7 (same issues on Windows XP)
On python 2.7 and Django-1.3.1, I successfully created a default project
django-admin.py startproject djsite
Now, I need to "bootstrap" djsite.manage as explained here (http://www.pyinstaller.org/wiki/Recipe/DjangoApplication) in a file called bootstrap.py located in djsite's parent directory as follows:
import djsite.manage
djsite.manage.execute_manager(djsite.manage.settings,['manage.py', 'runserver'])
Yet, as soon as the compiler sees:
import djsite.manage
I get this:
"Error: Can't find the file 'settings.py' in the directory containing 'C:\Python27\Lib\site-packages\djsite\manage.pyc'. It appears you've customized things... You'll have to run django-admin.py, passing it your settings module." And, I don't know how to follow the error's advice in this situation.
However, if I instead issue the following in bootstrap.py:
import os, sys
sys.path.append(os.path.abspath('djsite'))
import djsite.manage
djsite.manage.execute_manager(djsite.manage.settings,['manage.py', 'runserver'])
the script works correctly, but it breaks Pyinstaller (I've already asked this question on that software's mailing list (http://groups.google.com/group/pyinstaller/browse_thread/thread/174a72e26c26a44c). Even if I add the path to the djsite in my PATH variable, I get the same error.
So my question here is this: Why does importing the manage.py module fail with this approach and how can I proceed? Thanks!
Try adding this to your bootstrap.py to inform it where your settings file lives:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'djsite.settings'
This will assume that djsite is in your pythonpath. That is, if its located here: /path/to/my/djsite, then this should be in your pythonpath: /path/to/my
Actually the best way to be doing this from the start is to being using virtualenv which will ensure that your environment is correct. I feel like that had to have been part of your tutorial if I remember bootstrap at all. If you are using virtualenv, make sure you remembered to source bin/activate
If that doesn't work, you can try altering the runserver command:
args = ['manage.py', 'runserver', '--settings=/path/to/my/djsite/settings.py']
djsite.manage.execute_manager(djsite.manage.settings, args)
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.
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