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
Related
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
Hi everyone I'm trying to get Python configured on an OS X laptop and I'm having some trouble. I'm both new to Python and am very unfamiliar with the UNIX terminal. What I'd like to be able to do is to have a directory in my documents folder that would contain python modules and be able to run them from the command line. Currently I have a Python Directory and a chaos.py module inside of it. The full path is /Users/Ben/Documents/Python/chaos.py.
So I followed the steps here and here. I can see that the site-packages for Python 3.4 is in a few spots but I chose this one: '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages' to place the .pth file.
I created a file named Ben.pth in this location with the contents: /Users/Ben/Documents/Python/
Now from my (very limited) understanding that should be all I would need to do for Python to start looking right? So I try to run python3 chaos.py in terminal and I get an error:
/Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python: can't open file 'chaos.py': [Errno 2] No such file or directory
I'll also try opening IDLE clicking File->Open Module... and try to open it from there and I'll recieve a "module not found" box.
I'm completely stumped, I'm not sure if its a syntax error that I made somewhere (again I don't really know what I'm doing with the UNIX commands) or if I'm just way out in right field. If anyone could help me out, I would greatly appreciate it! Thanks!
Forget the .pth stuff for now, that's not something you'd normally do. In a unix-ish environment, the typical way you'd run a script would be to change directory:
cd /Users/Ben/Documents/Python/
and then run the script:
python chaos.py
Another way to do it would be to run the script with an absolute path; you can do this no matter your current working directory:
python /Users/Ben/Documents/Python/chaos.py
Finally, if you've written a utility script you want to be run from anywhere without typing that absolute path all the time, you can do something a little fancier...
Add a 'shebang' line as the first line of your script. It'll go like this:
#!/usr/bin/env python
Get into the directory where your script lives:
cd /Users/Ben/Documents/Python/
Make the script executable:
chmod +x chaos.py
Put a link to the script in a directory on your path... /usr/local/bin/ could be a good choice:
ln -s /Users/Ben/Documents/Python/chaos.py /usr/local/bin/chaos.py
Now you can type chaos.py anywhere on your system and it'll run.
I want to be able to run a python script at the command line using Enthought Canopy, but I don't want to specify the full path to the script.
As I see it, there are two options.
Option 1: Make the python script an executable, add #!/usr/bin/env python to the top of the script, and put the directory containing the script on my $PATH. Now I can execute the script like this:
$ run.py
Option 2: As suggested by Andrew Clark in another SO post, just put the directory containing the script on my $PYTHONPATH. Then I can execute the script like this:
$ python -m run.py
The -m causes python to search the $PYTHONPATH.
I prefer Option 2, and it works fine with the system python on my mac (v2.7.2), but I cannot get it to work with Enthought Canopy. I can load Canopy python and import modules in the same directory as run.py, so I know that I have the path correct. I just cannot execute the script from the command line. Is this a bug or am I doing something wrong?
BTW, it's probably a typo, but just to make sure you should be using the module name, not the file name, with the -m option. For example, python -m run
If that is not the problem then make sure that the python that is used in your option 2 is the python located in your Canopy User virtual environment. You can use the which command to verify that. For example:
$ which python
/Users/YourUserId/Library/Enthought/Canopy_64bit/User/bin/python
If that is not what you get then you can either add that bin folder to the beginning of your PATH environment variable, or you can activate that virtual environment like this:
source /Users/YourUserId/Library/Enthought/Canopy_64bit/User/bin/activate
I just downloaded Python 3.2 to Mac OS 10.6 environment. I'm new to programming and am trying to run my first stand-alone .py file, but I keep getting an error message saying "no such directory or file." The name of the file is "script1.py" and I saved it to /Users/billp/Documents. When I open the Terminal to run the file I type:
python script1.py
I've also tried adding this line to the beginning of the script:
#!/usr/local/bin/python
As well as this one:
#!/usr/bin/env python
Yet, I keep getting the same error message. Any help would be greatly appreciated.
Make sure you are in the right working directory after opening terminal. Type
cd /Users/billp/Documents/
(use tab to autocomplete)
then
python ./script1.py
This way you are launching python executable and passing it path to your file as the first argument. The shebang #! line you mentioned allows you to launch your script directly, like this: ./script1.py, but you need to mark the file as executable chmod +x script1.py and provide path to interpreter (python) after the shebang. #!/usr/bin/env python references your default python installation.
The ./ stands for current directory. By default when you type script1.py your shell (which is the thing that you type commands into, through the terminal) would look for executable file in special folders listed in PATH env variable. script1.py is not usually there, so you would see -bash: script1.py: command not found. python, on the other hand is on the PATH so it should work.
Maybe you forgot to make the file executable? Try this at the command prompt:
$ chmod +x script1.py
I prefer to start my Python scripts in a Mac with these lines (assuming of course that you're saving the file in UTF-8 encoding:
#!/usr/bin/env python
#coding=utf-8
Also, make sure that the pythoncommand is available in the path. If everything is set up correctly, it won't be necessary to type python first, and you can run the script directly by typing ./script1.py in the directory where it is located.
One final thing, for running a piece of code when executing the script from the command line (as opposed to simply loading the definitions in the file), write this at the end:
if __name__ == '__main__':
# the code you want to call
Are your python binaries here?
/Library/Frameworks/Python.framework/Versions/3.2/bin/python
It's worth pointing out, as long as the file is in your current directory it's automatically available. Otherwise, anything files will have to be referenced absolutely using the full path information.
So the following examples are calling the same file:
Explicit (absolute path)
python /Users/billp/Documents/script1.py
python /Users/billp/Documents/script2.py
python /Users/billp/Documents/script3.py
Implicit (relative path)
cd /Users/billp/Documents
python script1.py
python script2.py
python script3.py
As long as you're working with files in the same directory (commonly known as your working directory), you may always safely use relative paths. If the files are anywhere else, you must always specify an absolute path.