I added a folder to my PYTHONPATH where I can put all of my Django Apps. I print sys.path, and everything looks good, the folder I want is there. However, when I go to import a module, it tells me that there's no module by that name. All the site-packages modules work fine. In all of my Django apps, there's an "_____init_____.py" like there's supposed to be. I heard that if those are created on windows there can be problems, but I couldn't dig up much more than that.
Fixed it. Sorry Santa for not including the console print out. Windows was dumb and added an extra .py to the file when I created it. So everything was actually "pythonfile.py.py". Awesome.
Related
I have a module that I want to be able to import from any script without
doing any sys.add_path and similar stuff. I want it to be permanently added.
Since I installed Python with anaconda, I have a anaconda3 folder added to Path under
System variables. In the same place I added C:\Users\lukas\anaconda3\package and in the
package folder is my script with empty init.py, and the script.py which contains functions.
In the Path under User variables, I already had anaconda3 folder, and I added the \anaconda3\package.
Also, I created the PYTHONPATH variable under User variables and added anaconda3 and \anaconda3\package.
When I open my CMD, that is working in C drive, it can import the package successfully.
But, when I open the CMD in D drive(in my VS code) it is not working, can't import the package.
As you can see, I've tried everything I saw on the internet, and probably now I have much more junk than needed added paths.
How should I clean this up and make it work on every drive?
For anyone that is interested,
after several hours, I tried again(by accident) and
everything works :)
It seems like Win10 needed some time to really update the environment
variables.
Python3.7 on OS X 10.14.5
I have a python script that imports a single local module. They are in the same directory, and everything was working fine. It's a pretty simple setup.
I moved everything over to another directory (on the same machine) and local imports stopped working. The only thing I can tell that has changed is the path, which now contains a folder called "My Drive". I believe the spaces in the path are causing the problem with the import.
I've searched the web, and tried many things, but can't seem to get import to work with a folder in the path that contains spaces. Anyone know any solution to this or a resource to try to figure it out?
I cannot change the folder name.
EDIT:
This was the directory structure:
main.py
lab/
--labs.py
I was able to get it working by flattening out the directory structure. However, when using relative imports:
import lab.labs
or adding 'lab' to the path:
os.path.append("./lab")
import did not work in that location.
Symlinking also works, but that seems like running around python, rather than fixing it within python, and since this is code that will be shared, I don't want a single machine solution.
Python newbie here. Am using Anaconda (on Windows 7) by recommendation of a friend.
What I want to be able to do is make a change to my class in Notepad++ and then test it in my Python command prompt window immediately.
This is a simple question that I can't seem to find the answer: in what directory does a default installation of Anaconda expect me to be storing my .py files (so that I can easily load them using import <MODULE NAME>)?
My PATH variable is set to:
C:\USERS\<USERNAME>\Anaconda3;C:\USERS\<USERNAME>\Anaconda3\Scripts
(this is the default)
Am I supposed to be working out of the Scripts directory...? There's already a lot of files in there.
What do most people do? Perhaps add another folder to the PATH variable and work out of there? Do you add a new folder to PATH for each project or is there a way that makes more sense? I already have a Projects directory I use for everything else. I'd like to just be able to work out of there.
I am coding in Notepad++. I don't really want to bother setting up/learning an IDE (I'm just doing relatively simple I/O file manipulation that I have previously been doing in Excel... the horror).
Sorry for the extremely newbie question. I searched and could not find anything relevant.
EDIT AFTER ACCEPTED ANSWER:
The problem was that I was running python.exe from the Start menu. I did not realize that you are supposed to open a CMD window in the folder (SHIFT+RIGHT CLICK) you are working in (such as C:\USERS\<USERNAME>\MY PYTHON STUFF) and run python from there.
This might be what you're attempting. Note that I also use Anaconda.
My path:
C:\Users\...\Documents\Python Scripts\
import_sample.py
class class_sample(object):
def __init__(self):
self.x = ["I", "am", "doing", "something", "with", "Python"]
test.py
from import_sample import class_sample
c = class_sample()
y = c.x
print " ".join(y)
Result:
I am doing something with Python
[Finished in 0.1s]
Notice how being in the same folder allows me to import without having to install, per se. Basically, just make sure that the modules you need are in the same folder as your main.py and you're good.
EDIT:
Done from the terminal.
Notice how I cd into the above folder and activated python there. Since I was inside that folder, any modules inside it can be imported without any problems, alongside other system-wide modules installed as well.
I have a Django app and I'm getting an error whenever I try to run my code:
Error: No module named django_openid
Let me step back a bit and tell you how I came about this:
I formatted my computer and completely re-installed everything -- including virtualenv, and all dependent packages (in addition to Django) required for my project based on settings in my requirements.txt folder
I tried doing python manage.py syncdb and got the error
I googled the issue, and many people say it could be a path problem.
I'm confused as to how I go about changing the path variables though, and what exactly they mean. I found some documentation, but being somewhat of a hack-ish noob, it kind of goes over my head.
So my questions are:
What exactly is their purpose -- and are they on a system based level based on the version of Python or are they project dependent?
How can I see what mine are set to currently?
How can I change them (ie. where is this .profile file they talk of and can I just use a text editor)
Any input you would have would be great as this one is stumping me and I just want to get back to writing code :-)
The path is just the locations in your filesystem in which python will search for the modules you are trying to import. For example, when you run import somemodule, Python will perform a search for somemodule in all the locations contained in the path (sys.path variable).
You should check the path attribute in sys module:
import sys
print sys.path
It is just a regular list, sou you could append/remove elements from it:
sys.path.append('/path/to/some/module/folder/')
If you want to change your path for every python session you start, you should create a file to be loaded at startup, doing so:
Create a PYTHONSTARTUP environment variable and setting it to your startup file. E.g.: PYTHONSTARTUP=/home/user/.pythonrc (in a unix shell);
Edit the startup file so it contains the commands you want to be auto-executed when python is loaded;
An example of a .pythonrc could be:
import sys
sys.path.append('/path/to/some/folder/')
Do you really need to alter the path? It's always best to actually think about your reasons first. If you're only going to be running a single application on the server or you just don't care about polluting the system packages directory with potentially unnecessary packages, then put everything in the main system site-packages or dist-packages directory. Otherwise, use virtualenv.
The system-level package directory is always on the path. Virtualenv will add its site-packages directory to the path when activated, and Django will add the project directory to the path when activated. There shouldn't be a need to add anything else to the path, and really it's something you should never really have to worry about in practice.
i have a following solution structure in python:
main_app
main_app/template_processor/
main_app/template_processor/models
main_app/template_processor/views
everything works just fine on my local machine. as soon as code gets to server (it stopped working after i removed all .pyc files from svn), it doesn't see the assembly (if it could be called like that in terms of python) with models. during syncdb command, it creates no tables except for admin ones. but in runtime it finds models themselves, but does not find tables (since they were not created by syncdb)
i've added application to settings.py as installed app, everything seems to be fine.
the only difference that i can see for now is that on my local machine i have main_app/template_processor/models/models.pyc file, but it doesn't precompile it on server for some reason (might be a hint??)
__init__.py files are existing in every folder/subfolder.
have anyone faced an issue like that?
Hm, looking at your directory structure, I think that you need to import all your models in template_processor/models/__init__.py, because Django looks only in <app_name>.models module when loading models automatically (i.e. for syncdb).
My guess would be that you renamed a file, and didn't delete the oldname.pyc file.
So if you try to
import oldname
then you rename oldname to rename, but don't update your import statement, the code will work on systems where oldname.pyc exists, however python won't be able to recreate oldname.pyc if oldname.py doesn't exist.
try
find . | grep py | xargs grep import | grep -v django | sort -u
that should give you a list of all imports in your project, see if one of those imports is pointing at a module for which you have a pyc file but not a .py file.
In general, python quickly compiles .py files into .pyc files, and it does this once. I wouldn't worry about the time it takes to generate new .pyc files.
Sounds like Django isn't seeing that module (folder, in this case) for some reason. Make sure all the folders have a file called __init__.py (notice the two underscores before and after). Once that's done, make sure it's listed in your installed apps.
Maybe you made some change to it that's causing it to stop loading. You can also try moving the .pyc files out of the directory on your local machine and see whether they're regenerated or not when you runserver.
Maybe the most helpful thing: ./manage.py shell to bring up an interactive shell and then 'import app_name' (without quotes) to see whether django is having trouble finding the module.
Ok, if anyone's interested in what really was happening, i've got a story to tell ya:
http://code.djangoproject.com/ticket/4470
that's basically what i was going to implement.
in order to really get this thing work i still should have a file models.py, which will have a proper list of classess, with Pass inside of it. Then i should have taken all the files (my models) and changed their meta for syncdb to understand they are from the certain "assembly".
source code is available (pls see url above).
thx for helping out!