I've recently installed the windows bash. I tryed to make the packages installed with Anaconda visible by adding them to the PYTHONPATH variable but it is not listed in the environment.
Couriously enough sys.path.append works just fine and has entries, as sys.path doc states that the package variable is instatiated from the environment variable PYTHONPATH, but how can it be if it is not present in the environment (Bash of windows)?
I've already checked the windows environment and they're as separated system (otherwise the Anaconda packages would be available for import). So where can I set the python paths to new modules in windows bash?
If you are launching python from windows bash it won't automatically find your PYTHONPATH environment variable from windows. You need to add it yourself. You can do this by editing the .bashrc file. It is located in your home directory (/home/userid or just ~).
vi ~/.bashrc
Now add the following line:
export PYTHONPATH="/mnt/c/my/python/path"
If you are using some IDE like PyCharm, it may create it's own PYTHONPATH. It certainly won't be visible from system.
You may create PYTHONPATH variable on your own from system settings (Control Panel etc). But if something like IDE then replace it locally, your modules won't be able to import.
The stable but a bit rude way is modify the windows registry by modifying a value in PYTHONPATH record.
The code that uses RapidEE utility (RAPIDEE_EXECUTABLE is path to your rapidee.exe) to achieve that
def set_pythonpath():
subprocess.call([RAPIDEE_EXECUTABLE, '-S', '-C', 'PYTHONPATH', YOUR_PACKAGE_PATH])
Related
I am using Python 3.7.9 Shell.
I created a virtual environment in this location
C:\Users\my_username\Desktop\Projects.venv
Inside of Python Shell, when I type: import numpy, which is in my .venv\lib folder, it says that the module does not exist.
Using Python Shell, how do I make use of the contents in .venv? In particular, the libraries located there?
Edit #1: Include Details
In my windows command line, it has (.venv) off to the left.
I have run the Activate file. I then started Python.
In my \lib\site-packages area, I have the requests library.
When I open up Python Shell and type "import requests", it says "no such library can be found"
I am using Windows 10
I installed the libraries while in the (.venv) environment.
Theory:
In the virtual environment, in Python Shell, it's searching a different location for libraries...now if I can just figure out where it's searching and how to change that...I might be able to make progress.
Edit #2: My Progress
My theory was correct. Despite using a virtual environment, it's not looking for the libraries installed in (.venv)\lib\site-packages, it's looking somewhere outside of that.
Now I just need to figure out how to make the Python code look for libraries inside of (.venv)\lib\site-packages when I'm in the virtual environment.
When I run the python.exe file inside of the (.venv)\Scripts location, it recognizes the virtual environment scripts.
If I click on my version of Python.Exe located in my C:...\Programs\Python 3.7 folder, it doesn't recognize them.
I was under the impression it didn't matter where I clicked on the Python.exe file if I did it after going to the virtual environment in the command line? Is this not true?
Edit #3: Important Links
Where Python Looks for Modules When Importing
Right from the official docs https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments
Once you’ve created a virtual environment, you may activate it.
On Windows, run:
tutorial-env\Scripts\activate.bat
On Unix or MacOS, run:
source tutorial-env/bin/activate
this is done in your shell before starting python at its prompt, and allows you to choose different python versions in addition to other benefits
Good day!
Installed the Python 3.9.1, checked "Add to path", the cmd did not work though.
Added Environment Variable Path, both folder
C:\Users\XXXXX\AppData\Local\Programs\Python\Python39
(file manager opens the path to python.exe just fine)
and script lines:
C:\Users\XXXXX\AppData\Local\Programs\Python\Python39
Still the commands python -version and pip --version do not work from the command line.
Py --version works just fine though.
Anyone might share and idea what might be the reason?
This happens more often than one would think.
When you installed python from python.org and follow the installer, post install, you should check your environment variables, and verify that you have no other python installation (or if you do just name them appropriately).
Often you can find that there is an Environment Variable, that includes a python version on the global Environment Variable Path.
To verify that you are targeting the correct python version put these two directories (replace, user and python version), at the top of your PATH (user's path), and check that there are no conflicts with the rest of the PATH variables.
C:\Users\XXXXX\AppData\Local\Programs\Python\PythonXX
C:\Users\XXXXX\AppData\Local\Programs\Python\PythonXX\Scripts
If you had Python installed in the system before, the new path is added at the end of PATH system variable and when system looks for python.exe it finds first the old version that is available under a different folder.
If you used a command window opened before the new version got installed, it is also possible that system variables did not reload. Close it and use a new one to check.
I am using Windows 10 and have Python installed. The new update brought bash to windows, but when I call python from inside bash, it refers to the Python installation which came with the bash, not to my Python installed on Windows. So, for example, I can't use the modules which I have already installed on Windows and would have to install them separately on the bash installation.
How can I (and can I?) make bash point to my original Windows Python installation? I see that in /usr/bin I have a lot of links with "python" inside their name, but I am unsure which ones to change, and if changing them to Windows directories would even work because of different executable formats.
As of Windows 10 Insider build #14951, you can now invoke Windows executables from within Bash.
You can do this by explicitly calling the absolute path to an executable (e.g. c:\Windows\System32\notepad.exe), or by adding the executable's path to the bash path (if it isn't already), and just calling, for example, notepad.exe.
Note: Be sure to append the .exe to the name of the executable - this is how Linux knows that you're invoking something foreign and routes the invocation request to the registered handler - WSL in this case.
So, in your case, if you've installed Python 2.7 on Windows at C:\, you might invoke it using a command like this from within bash:
$ /mnt/c/Python2.7/bin/python.exe
(or similar - check you have specified each folder/filename case correctly, etc.)
HTH.
I do not have Windows 10 installed, but I use Babun and I had the same problem. As I read aliases work well in Windows 10 shell so simply add alias in your .bashrc pointing to your Python installation directory:
alias python /mnt/c/Python27/python
You have at least four options:
Specify the complete absolute path to the python executable you want to use.
Define an alias in your .bashrc file
Modify the PATH variable in your .bashrc file to include the location of the python version you wish to use.
Create a symlink in a directory which is already in your PATH.
I am trying to set up a virtual environment on my machine, and I am really having trouble.
EDIT: I set up my virtual environment by installing virtualenvironment1.9 and running:
python virtualenv.py -p myVE
Then I add the line
alias goVE='source ~/virtualenv-1.9/myVE/bin/activate'
to my bashrc, and I use goVE to activate the virtual environment. I get a (myVE) at the beginning of the commandline in terminal, so I think it's working. It's just losing the gtk module somehow.
END EDIT
Basically, if I try to
import gtk
in python, it tells me there is no module named gtk. But outside my virtual environment, gtk imports with no problem. I decided it was a problem with the path, so I added
export VIRTUALENV_EXTRA_SEARCH_DIR=$PYTHONPATH
export VIRTUALENV_EXTRA_SEARCH_DIR=/usr/lib64/python2.6/site-packages/gtk-2.0
to my bashrc. Since evidently the gtk module was found in the pythonpath, I thought the first line would do it. When that was insufficient, I tried adding a path specifically to the directory where gtk was located, which I verified by trying
import gtk
gtk.__file__
Since this still didn't work, I tried modifying the virtualenvironment/bin/activate file, so:
PATH = ="$VIRTUAL_ENV/bin:$PATH:/usr/lib64/python2.6/site-packages/gtk-2.0/"
Again, this should be included in the original PATH (unless I'm mistaken), but I'm getting desperate so I tried adding it explicitly to the PATH. For completeness, my PYTHONPATH is set by:
export PYTHONPATH=${PYTHONPATH}:/data/monroe/ebexcode/trunk/GetData/:/home/user1/geach/:/usr/lib64/python2.6/site-packages/
You haven't explained how you created your virtual environment so there's no way to tell if that succeeded. If your virtual environment was created successfully, there's no need to manually munge the paths.
Assuming you have a virtual environment, you haven't demonstrated that you were able to activate it. In order to activate it, you have to run a script in the virtual environment's bin directory. Example, if your virtual environment is in ~/myvenv and you run bash, activate your virtual environment by issuing this at your prompt:
source ~/myvenv/bin/activate
At this point you can use this instance of your shell to install packages into your virtual environment:
pip install PyGTK
Depending on your system installation, this might fail if you do not have all the tools and libraries installed to build the Python GTK bindings.
I'm using PyCharm on a mac (OSX mavericks) to run a simple script shown below. All it does is print the PATH variable. I have a virtualenv in the project directory. I added a Run Configuration in PyCharm and tried it with different Pythons:
# file mytest.py
import os
print "PATH: ", os.environ['PATH']
When I run with the system default python (/usr/bin/python) it prints the correct value for PATH (i.e. the PATH as I have configured in my .bash_profile file,) which is kind of long and contains many directories.
But when I choose the venv's Python, the path is reduced to only: /usr/bin:/bin:/usr/sbin:/sbin:/Users/myname/projects/myproj/venv/bin
This doesn't happen if I run the script from a terminal window. In this case it shows the correct PATH for both the system's python and the venv python. It also doesn't happen if I deactivate the venv and run venv/bin/python mytest.py.
Anyone knows how to make the correct PATH value be set when running from PyCharm and using venv?
you should probably know that all environment variables are inherited.
When you define environment variable in your .bash_profile it becomes available in your terminal (bash),
and in all processes that will be started from terminal (These processes will be children for the bash process).
That's why you are getting expected values when running your script from within the terminal.
You start PyCharm not from a terminal, so it doesn't inherit PATH. And so do Python or venv (they launched from PyCharm).
To solve your issue you have 3 options here: just start PyCharm from terminal or move PATH variable definition from .bash_profile to session init scripts (PATH will be defined system-wide) or Duplicate your PATH in PyCharm's run configuration (it has such option over there)
Good luck!
I built something for SublimeLint that figures out your shell's PATH in Python.
https://github.com/lunixbochs/sublimelint/blob/st3/lint/util.py#L57
Basically, it runs $SHELL --login, echoes your path, and parses it.
The gist is:
import os
import subprocess
shell = os.path.basename(os.environ['SHELL'])
output = subprocess.Popen(
(shell, '--login', '-c', 'echo __SEP__$PATH'),
stdout=subprocess.PIPE).communicate()[0] or ''
print output.split('__SEP__', 1)[1].split(':')
The problem for me on windows was case related: it's the difference between Path and PATH.
If you choose:
Edit configurations
Environment Variables
Show (down in the bottom right corner)
This shows the environment variables. On my system there is no PATH, only Path.
Defining a new PATH environment variable wouldn't work, but Path worked fine.
There's a workaround in my case.
To modify the environment variable PATH, I usually just need to add extra paths to the PYTHONPATH variable in the run configuration's environment variables setting.
PYTHONPATH will then be added to PATH.