I have two python executable, one in Anaconda env and one in WSL(Obvisouly, I'm using Windows).
And there are two files, one is test.sh and the other is test.py.
Both files are simple (I made those for troubleshooting)
# test.sh
python test.py
# test.py
import sys
print(sys.executable)
When I execute test.py, the path is like this.
C:\Users\user\anaconda3\envs\dassl\python.exe
However, when I execute test.sh, the path is like this (Obviously it points to the python in WSL)
/usr/bin/python
Even though I don't know much about WSL, I guess that by using bash command in WSL, python in WSL rather than the one in the anaconda is pointed. I'm struggling with this problem because I can't use pytorch package installed in conda env with a bash command.
Is it impossible to use conda with base command of WSL?
Related
a colleague of mine has written a python script that I need to use, which is called within a shell script. It produces plots with matplotlib. However, when I try to run his script, it fails in matplotlib commands with "ImportError: No module named PyQt4". The python script is called within the shell script with a syntax like
./script.py
script.py begins with a line to specify the python exec to use from within his miniconda environment, like
#!/user/miniconda/envs/py27/bin/python
I think the problem is that the code uses the default PyQt on my system when I run this command. I tried running script.py with the python exec in his environment, but this gives the same error. This also occurs if I try to run the script on his computer when logged into my account. Is there a way that I can run this script as if I were my colleague within my account?
Have your colleague generate a yaml file with his environment dependencies, then create a copy of his environment on your computer to run the script.
# your coworker runs:
conda env export -n [name of his environment] > environ.yml
Once you get yaml file, you can run
conda env create -f environ.yml
to copy the environment. From there, activate it and run the script
# on Windows
activate [environment name]
python ./script.py
# on *nix
source activate [environment name]
python ./script.py
I'm grading projects written in Python. Some are written in Python 2, some in Python 3. I'm grading them through the command line for simplicity, but right now, projects written with Python 2-specific syntax won't work because the interpreter defaults to Python 3. Is there an easy way to designate which version of Python I want to use on the fly?
You could use Python launcher on Windows:
C:\> py -2 some_script.py
If the script has the shebang such as #!/usr/bin/env python3 then the launcher finds the appropriate Python version automatically:
C:\> py some_script.py
Python launcher is included in the recent Python versions (since 3.3). You could install it separately otherwise.
You could also use vex utility with virtualenv:
$ vex py2 python some_script.py
that runs python some_script.py in py2 virualenv.
On POSIX systems (Linux, OS X) if the script has executable permissions ($ chmod +x some-script) and it has a valid shebang such #!/usr/bin/python then you could run it directly:
$ ./some-script
Which python version (or even which program) will be used is defined by the shebang.
The best way is to use a virtualenv.
The second best way is to just setup your system to use python for python 2 and python3 for python3.
Use aliases:
On windows its:
set "py2= path-to-python2-interpreter"
then call with:
%py2%
On Mac its:
alias py2='path-to-python2-interpreter'
then call with:
py2
On linux, its the same as on a Mac.
Note: these aliases wont save after you close the terminal window.
If you want them to save you need to put them in whatever config file your shell reads on start-up.
I only know the one for linux and its ~/.bashrc
The best way is to use the conda distribution which is available on all platforms. This makes easy to switch environments even with different python versions. For example:
conda create -n nameofpython2environment python=2 # creates a python 2 env
conda create -n nameofpython3environment python=3.3 # creates a python 3 env
Then to work in a given environment just do:
source activate nameofpython2environment
Once you are inside an environment you can install packages there:
conda install numpy
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'm using a virtualenv to execute a script, in this script I call:
os.system('python anotherScript.py')
My question is whether the script is executed in the same virtualenv as the caller script?
It's hard to tell, but if you are running this script under an activated virtualenv, you should be under that virutla environment. You can verify your thought by doing
#script.py
import os
os.system('which python')
and from command-line
virtualenv newvirtualenv
source newvirtualenv/bin/activate
(newvirtualenv) user#ubuntu: python script.py
you should see it is under newvirtualenv/bin/python
Usually, you want to put an exectuable header to use the current environment:
#!/usr/bin/env python
import os
os.system('which python')
This does not say use newvirtualenv, but gives you a little more confident if the script is executed under newvirtualenv, it will definitely be newvirtualenv.
If you use /usr/bin/python this is still okay under virtualenv. But for advanced programmers, they tend to have multiple virtual environments and multiple python version. So depending on where they are, they can execute the script based on the environment variable. Just a small gain.
If you run newvirtualenv/bin/python script.py it will be under virtualenv regardless.
As long as the python binary is pointing at the virtualenv's version, you are good.
e.g. use anaconda to manage virtual envs, and in Pycharm IDE:
os.system('which python') # /usr/bin/python
command = 'python3 xxxx.py'
os.system(command) # make /usr/bin/python as interpreter
If I want to use some modules (e.g. cv2) installed in certain virtual env,
command = '/path/to/anaconda3/envs/your_env_name/bin/python3 xxxx.py'
os.system(command)
I have 2 versions of python installed on windows, 2.7.3 and 3.3. Some of my scripts are 2.x and some 3.x. Is there an easy way when executing these scripts from a command line to direct them to the appropriate interpreter?
Note: For Windows use the new Windows Python launcher (available with Python 3.3 and downloadable here for earlier releases) which recognizes Unix shell shebangs. You can read about it here.
Most Linux distributions will create python2 and python3 aliases for the installed Python 2.x and Python 3.x interpreter (if not you can just create the symbolic links yourself anywhere on your $PATH, the env command will take care of finding them), so you should just need to set the appropriate interpreter as the first line of your script:
#!/usr/bin/env python2
or
#!/usr/bin/env python3
This will direct the shell to use the appropriate interpreter, if you set the script files to be executable and just invoke them directly on the shell. E.g.:
$ chmod +x script.py
$ ./script.py
Try this first: I am on OS X, but when I want to use Python 2.6 instead of Python 2.7 (its a numpy/scipy thing), I simply run python2.6 whatever.py to run whatever.py in Python 2.6. Try that first.
If that doesn't work, then you can use virtualenv - the virtual environment builder for Python.
http://pypi.python.org/pypi/virtualenv
I am sure there are similar alternatives, too.
Pedro Romano's answer is the most elegant way of doing this.
But if you don't want to download and install the Python launcher, create a batch file as described below. You can also create a shortcut, copy C:\Python27\python.exe to C:\Python27\python27.exe, etc.
I'm guessing C:\Python27 and C:\Python33 are already on your system path. If so, you can create a batch file called python2.7.bat in C:\Python27\ which contains:
C:\Python27\python.exe %1
and a similar file (e.g. python3.3.bat) in C:\Python33\
Now, you can run python2.7 script.py from anywhere in command prompt and it should work :)