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.
Related
I know there must be a really obvious answer, but it eludes me. Where does "/folder1/folder2/file.ext" save to in Python? In some code I thought (incorrectly) that it would save the file to a relative path, but when I checked folder2 it wasn't there. However, it seemed to still take up the hard disk space. I did a search on my hard drive and couldn't find the file. Where did it go? Again, I'm aware that there must be a really obvious answer out there, but I am just learning to code.
Firstly I would read about the os module and the module search path.
I opened the IDLE 3.7.0 on my Windows 10 machine and did the following:
>>> import os
>>> os.getcwd()
'C:\\Python37'
>>> os.path.abspath('./folder1')
'C:\\Python37\\folder1'
Windows expands the . to represent the current working drive, thus wherever the interpreter is looking is where your folders will reside.
In the links, it talks about how sys.path works (in short the directory containing the current script is placed at the front of it [e.g. the current working directory] for searching).
So, in short, either run your script directly from the directory or call os.chdir('/your/desired/path').
I wrote a small python program and again I am struggling with producing a good structure.
A few days ago I read a blog and many other websites, which advise against from a import b imports and recommend to use always import a.b "project absolute" path (the whole path from the project root to what I want to import) imports. That as a background.
I included a __main__.py so that I can run my program with python -m <directory>, which was another recommendation of someone on stackoverflow in one of the hundreds of python import questions. It is supposed to help keeping code runnable and testable with the same import structure, which was a problem in another project of mine.
Now what I want it, that from anywhere in my system, I can run python -m <dir of my code> and not only from one directory up the RSTCitations directory.
How can I achieve that, without:
python path manipulations (which are a dirty hack)
changing my imports somehow and getting a not recommended import structure
doing other dark magic to my code
I want to stick to best practices in organizing my code but still want it to be runnable from wherever I am in the terminal.
Example of fail
When I run the program as described from another directory completely unrelated to my program, I get for example the following error:
/home/user/development/anaconda3/bin/python: No module named /home/user/development/rst-citations-to-raw-latex/RSTCitations
However the path is correct. That is exactly where the code is.
You can :
install your program with pip ( see Installing Python packages from local file system folder with pip )
put your module in the python import path (and/or edit PYTHONPATH in the environment for the users that need to use it)
If you don't need other users to 'import' your library but just use it as a standalone program, you can also just put a symlink/script to your program, makeing it runnable from a directory which is in your PATH.
I'm contributing to a Python 3 project from a Windows 7 computer.
In order to minimise clutter while making and testing changes, I'm placing a few test files inside a folder called ignore within the project, and excluding it from the version control system (Git).
There is no __init__.py file in the root of the project, so I thought I'd create a symbolic link from the module I want to test into the ignore folder, to enable me to import it.
mklink ignore\example.py example.py
When I try to import the module from a script in ignore I get an ImportError saying the example module doesn't exist.
Is it possible to import modules via symbolic links in this way? I understand that this is supposed to work, but I can't find a reference saying that it works on Windows.
I have just spent +2 hours trying to resolve this issue and
it turned out that creating a Windows Directory Junction worked for me.
You don't need to use a relative one, it worked with both.
So first create an empty file __init__.py in your igonore-folder
then in the elevated mode (As Administrator) create a link:
mklink /J ignore path-to-your-ignore-module
Then in your Python code you would be able to execute:
from ignore.something import some-other-thing
I am using pycharm version 3.4 on ubuntu and have a project with the following structure:
~/project/src/python/utils/sub1/sub2/sub3/my_code.py
the utils folder also contains an __init__.py file which offers a number of utility functions. I want to include some of them, but it wouldn't find it:
from project.src.python.utils import read_utf8
I followed this post, which seemed to discuss the same problem:
PyCharm can't find the right paths if I open a directory that is not the Django root
But changing ~/project to a "source" folder didn't help. This isn't a typo on my side, as it should at least find "project" when trying to import something from it, but
import project
also gives my an "unresolved reference" error.
edit: I should add that I cannot change the code, as it is a shared project. I need that exact import line to work on my machine. My counterpart uses eclipse, were it seems to be easier to add a path to additional code.
It looks like the source isn't in your PYTHONPATH which is why you're unable to import it.
python only looks in certain places for py/pyc/pyo etc. files and modules to import (the places are the paths listed in sys.path). You can add custom places for it to look with the PYTHONPATH environment variable, or in PyCharm under preferenes > project interpreter > configure interpreters, and then adding any paths in the paths section.
I can't tell which folders are actually modules but from the names of the folders I would guess only utils. If that's the case add the path /home/ceca/project/src/python to the list of paths in PyCharm and in your code from utils import read_utf8
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.