I wrote one python script that makes use of some libraries I contained in a different folder. To access them what I've done is adding at the beginning of the script the following lines:
import sys
sys.path.insert(1, 'library_dir')
This is working fine when I'm using my python IDE but when I run the code from the terminal it fails in importing the libraries I'm interested in.
How can I solve the problem?
This is a relative path problem.
The path you are inserting into sys.path is relative to the location that your run the script from. Your IDE will run it from a certain directory (probably the directory of the "project" in that IDE). If you run it from that same directory on the command line, it should work.
Alternatively, you can specify an absolute path:
Windows:
sys.path.insert(1, 'c:\\path\\to\\library_dir')
*nix:
sys.path.insert(1, '/path/to/library_dir')
Set the environment variable "PYTHONPATH" before calling the python script. In bash that can be done with:
$ export PYTHONPATH="/some/default/path:${HOME}/library_dir"
Related
I have Python 3.7 on my path (I can execute .py scripts when I am in that local directory in cmd)
I also have a folder of scripts on my path (I can open them from any local directory in cmd i.e. by typing "script.py")
However, I cannot execute these scripts from any local directory explicitly using python, i.e. "python script.py"
Any ideas why this is the case? Thanks
Edit:
The desired folder "scripts" is set in PYTHONPATH variable, and checking within python I see
import sys
sys.path
['', 'C:\Users\benma\Desktop\scripts',...
I can import a file from scripts into python already running, but not execute it directly
Python doesn't search PATH to look for your scripts. You can run the script directly because the shell is searching PATH looking for something that matches.
PYTHONPATH won't help when executing from the shell. It is only used by Python when importing modules:
Augment the default search path for module files.
I don't think you're going to get exactly what you're after. The closest is probably executable modules.
I have Python 2.7 installed at C:\Python27 and I have added the path C:\Python27\; to the environment variables and .py: to PATHEXT. I am able to launch Python.
I downloaded a folder google-python-exercises to my desktop, which contains a script hello.py.
Following the advice in the Google Developers course, I try to run the script by using python hello.py at the command prompt.
When I attempt this, I get the message: python: can't open file 'hello.py: [Errno 2] No such file or directory. What is wrong, and how am I supposed to fix it? I found that I can solve the problem by running cmd from the folder, but this seems like a temporary solution.
Python cannot access the files in the subdirectory unless a path to it provided. You can access files in any directory by providing the path. python C:\Python27\Projects\hello.py
I resolved this problem by navigating to C:\Python27\Scripts folder and then run file.py file instead of C:\Python27 folder
Options include:
Run the command from the folder where hello.py is located (this way, hello.py is already a relative path to the file). This is the solution that OP found.
Give a proper path to the hello.py file - either absolute (e.g. C:/Users/me/Desktop/google-python-exercises/hello.py) or relative (for example, google-python-exercises/hello.py, if the current working directory is the desktop).
Add a path to the folder (C:/Users/me/Desktop/google-python-exercises) to the PYTHONPATH environment variable, and run the code as a module (python -m hello).
In all cases, a path is being given directly - Python will not "search" for the file.
From your question, you are running python2.7 and Cygwin.
Python should be installed for windows, which from your question it seems it is. If "which python" prints out /usr/bin/python , then from the bash prompt you are running the cygwin version.
Set the Python Environmental variables appropriately
, for instance in my case:
PY_HOME=C:\opt\Python27
PYTHONPATH=C:\opt\Python27;c:\opt\Python27\Lib
In that case run cygwin setup and uninstall everything python.
After that run "which pydoc", if it shows
/usr/bin/pydoc
Replace /usr/bin/pydoc
with
#! /bin/bash
/cygdrive/c/WINDOWS/system32/cmd /c %PYTHONHOME%\Scripts\\pydoc.bat
Then add this to $PY_HOME/Scripts/pydoc.bat
rem wrapper for pydoc on Win32
#python c:\opt\Python27\Lib\pydoc.py %*
Now when you type in the cygwin bash prompt you should see:
$ pydoc
pydoc - the Python documentation tool
pydoc.py <name> ...
Show text documentation on something. <name>
may be the name of a Python keyword, topic,
function, module, or package, or a dotted
reference to a class or function within a
module or module in a package.
...
Try uninstalling Python and then install it again, but this time make sure that the option Add Python to Path is marked as checked during the installation process.
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
im pretty new to all of this so please try to bear with me.
I've got a directory set up where i dump all the scripts im working on,
and i'm trying to make it so that i can run the scripts from within that directory directly from ipython.
so far, ive add an init.py to the aforementined directory,
and have tried appending the path to sys.path,
however, even after i successfully append the path, trying to use the run command for any script in the directory results in a not found error.
another problem i have, is that after every kernel reset the sys.path seems to reset to its previous values, without the new path settings i enter.
grateful for any help,
ron
sys.path only affects imports, not IPython's %run. The run magic is like calling python script.py - you have to cd into the directory where scripts are, or pass the full path to those scripts.
If you just want to run scripts with ipython from a directory, you could put ipython in the shebang and add the directory to your path, like so:
The script would be like this (python3 example):
#!/usr/bin/ipython
print("HI from ipython")
And then you'd add the directory where the script is to the PATH enviroment variable (if you're using Linux or other *NIX):
export PATH="/home/ron/ipython_scripts:$PATH"
Now, you should make them executable:
chmod +x /home/ron/ipython_scripts/script.py
Now you can use the script anywhere :)
$ script.py
HI from ipython
in Ipython notebook type : %run script_name.py
I have an exercise for a course in Python I have to complete. I have saved my methods/defs in one file and just need to figure out how to run it. I was hoping you could explain to me how to import files (I know the syntax"import filename"). When ever I do this I get an error. How do I change the file path of the import to the file on my desktop? I am using a mac and running IDLE 2.7.3
If the files are in the same directory as that file you can just use
import <filename> #(without the <>)
However, if you are referring to the files in a separate directory use imp
import imp
module = imp.load_source('module.name', '/path/to/file.py')
module.SomeClass()
On Mac OS X, there are two basic ways to launch IDLE. One is by double-clicking on an IDLE icon from the Applications folder in the Finder. (I'll call that IDLE.app) The second is to launch IDLE from a terminal session shell with something like:
idle2.7
I'll call that bin/idle because it refers to file in one of your system's bin directories.
When launching bin/idle, IDLE will inherit the current working directory of the shell and will add that directory to the front of the list of directories Python searches for imports. You can examine that list of directories in the IDLE shell window:
import sys
sys.path
When you launch IDLE.app, however, it is not associated with any shell so there is no opportunity to tell IDLE which working directory to use. In this case, IDLE.app uses your Documents folder/directory as its working directory: in shell notation, ~/Documents, also spelled /Users/your_login_name/Documents.
You can manually manipulate sys.path in your Python program to add another directory for imports:
import sys
sys.path.insert(0, '/path/to/directory')
Another way to manipulate sys.path is to use the PYTHONPATH environment variable. But, again, when using IDLE.app, there is no (easy) way to pass environment variables to it, unlike with bin/idle
So if you want to use IDLE.app, the simplest approach is to put into your Documents directory all of the files and packages directories that you want to be able to import. Otherwise, use a terminal session, set the desired working directory and/or PYTHONPATH variable and launch bin/idle. So something like:
cd ~/MyProject
export PYTHONPATH=~/"AnotherPackageDirectory"
idle2.7
Yet another approach is to install your modules in the default locations that Python searches and use Distutils or easy_install or pip. But that's something to learn about later when you have a finished project.