How are Virtualenv site-packages created? - virtualenv

I am using pipenv on Mac and see some site-packages installed in the ~/virtualenv/python3.9/. In my case there is an OpenSSL and I'm not sure how it got there seeing as there is no corresponding entry in pipfile. I take this to mean it is duplicated from the system or it was added with pipenv then remove from pipfile?

Related

VS Code cannot find virtual environment on the interpreter, but can find it on integrated terminal

I have a fully working virtual environment installed on my Linux machine.
This venv can be regularly used by the terminal in VS code calling source /mypath/venv/bin/activate.
The problem is that the Python interpreter in VS code cannot access any of the packages in the virtual environment, despite setting up the path on the interpreter as described in most of the guides.
I decided to manually set up the path in the settings.json file inside the .vscode folder as follows:
{
"python.pythonPath": "/mypath/venv/bin/python3.8"
}
venv is still not accessible through the interpreter. Any other suggestions?
I will answer my own question.
Turned out my pip installation was pointing to a path (standard /home/username/.local/bin/pip) which was different from my venv directory (/my_path/venv/bin/pip).
You can display the path by executing the command which pip.
In my very specific case, there was some mix-up when I first setup my Linux machine, meaning that venv only had a small amount of packages installed, while the directory containing the Python libraries and actually being used was the pip path. In other words, activating venv did not make any difference, since the Python libraries where loaded from the pip path.
So, first I had to ensure that pip had to point to the my venv folder, by modifying the .bashrc file in /home/username/, replacing
export PYTHONPATH=/home/username/.local/lib/python3.8
export PATH=/home/username/.local/bin:$PATH
with
export PYTHONPATH=/my_path/venv/lib/python3.8/
export PATH=/my_path/venv/bin:$PATH
All I had to do after was re-installing each of the required packages in the newer venv (generating a requirements.txt file from the older pip path helped).
Then I selected the venv path in the VS Code interpreter and everything is working fine now.
You actually do not need the settings.json file.
You could try to remove the .venv folder and create a new one by
python -m venv .env
It seems that vs code have changed something from .venv to .env. I'm not sure why.
After doing python -m venv .env open the terminal in vs code and it will active your .env.
You could (if you froze your pip installations) do a pip install -r requirements.txt and you are all good to go.
The default for the "python.envFile" setting is "${workspaceFolder}/.env" change it to "${workspaceFolder}/.venv" and restart vscode.

VSCode unable to import python libraries

i am using vscode and in the python environment setup, whenever i am able to run the script it is showing the import error.
the compiler is failed to import the libraries
How are you installing your dependencies?
If it is just through local pip, then running pip install or pip3 install within the same directory as your requirements.txt file should work.
If you are using poetry, then it might get a little involved.
Again, if these are all local, first try running poetry install within the same directory as pyproject.toml file.
If you are using virtual environments with poetry, try:
Run the command poetry shell to get all of the values you need in order to update vscode's preferences.json file (to get to this file, open Code > Preferences > Settings and then click the document icon in the upper right corner)
When you see "Spawning shell within...." copy/paste that path into virtual environment path "python.venvPath": "<Virtualenv.Path value>"
While inside of the shell, type which python to get your second value, python path "python.pythonPath": "<Virtualenv.Python value>"
Last, click on the python version number in the status bar of your vscode window and make sure to set it to the value that's in step 3.

How do I install the Matlab MCR in Ubuntu 14.04 without "killing" Unity?

Background: I have created an Ubuntu VirtualBox from LAPP stack and added the Ubuntu desktop (Unity: sudo apt-get install ubuntu-desktop). Now I am attempting to install the MCR without loosing Unity.
Download MCR zip and extract to MCR_SOURCE
Go to my folder that contains the files: cd /media/sf_shared/MCR_ SOURCE
Change installer_input.txt file:
destinationFolder=/opt/MCR
agreeToLicense=yes
outputFile=/opt/install.log
mode=silent
product.MATLAB
product.MATLAB_Builder_JA
# Note: To find out the required toolboxes >> start Matlab >> run your code and find out which toolboxes were used with: license('inuse')
Install MCR: sudo ./install -inputFile /media/sf_shared/MCR_SOURCE/installer_input.txt >> success
Restart Ubuntu >> test whether Ubuntu’s Unity still exists >> everything is fine
Attention the next step will “ kill ” your Ubuntu desktop configuration!!! (i.e. copy your hardisk, anything you must do to recover quickly) – now configure: sudo gedit /etc/environment
LD_LIBRARY_PATH="/opt/MCR/v84/runtime/glnxa64:/opt/MCR/v84/bin/glnxa64:/opt/MCR/v84/sys/os/glnxa64:${LD_LIBRARY_PATH}"
XAPPLRESDIR="/opt/MCR/v84/X11/app-defaults"
# Note: X11/app-defaults folder has not been created during installation
Restart Ubuntu >> Unity is gone, recovery attempts such as deleting the above lines do not recover Unity; reinstalling the Ubuntu desktop does not help either.
I have tried an alternative route with exporting the variables, which also "kills" Unity. By the way this affects all users.
Any ideas?
It is not necessary to register these environment variables in /etc/environment, which means that the Unity sidebar will not be affected.
Instead register the environment variables temporarily either as local user or via sudo -i:
export LD_LIBRARY_PATH="/opt/MCR/v84/runtime/glnxa64:/opt/MCR/v84/bin/glnxa64:/opt/MCR/v84/sys/os/glnxa64:${LD_LIBRARY_PATH}"
export XAPPLRESDIR="/opt/MCR/v84/X11/app-defaults"
Now it is possible to run Matlab Apps without "killing" Ubuntu's desktop. For instance to run the Java compiled makesqr.m file.
java -classpath "/opt/MCR/v84/toolbox/javabuilder/jar/javabuilder.jar:/media/sf_shared/for_testing/makesqr.jar" makesqr.Class1 5
The Java package makesqr was created using Matlab's JavaBuilder tutorial. This was done on my Windows 7 machine, which runs Matlab R2014b.
Please ensure that the owner and permissions of the /opt/MCR and /media/sf_shared/for_testing folders are set correctly (see here for details).

Installing with PIP in virtualenv?

I'm trying to wrap my head around virtualenv and pip still.
If I use pip to install a library, it doesn't matter where I 'cd' to, because it installs the libraries in the same place right (which i dont even know where that is)? So I guess my question is, when I install something with pip, how do I make sure it only installs that library inside of my virtual environment? Do I need to cd to that directory first? or is there a command I'm supposed to use with pip to make sure it only installs to the virtualenv project I'm working in?
Activate virtualenv first:
source virt_name/bin/activate
Then after that, install the libraries:
pip install module_name
Note: Don't use sudo with pip because sometimes it will assume you want to install in /usr/local/lib/site-packages.
Generally speaking, if you do not use virtualenv --system-site-packages to create your virtualenv, you should be only working with your per-environment packages.
Providing you run the activate script before installing anything.
i.e. Do the following, if you want to install something in your virtualenv.
Run activate script
Windows: [ve_directory]\Script\activate.bat
Linux: source [ve_directory]/bin/activate
pip install [your requirements]
I think it doesn't matter where your current working directory is.
Reference:
http://www.virtualenv.org/en/latest/#the-system-site-packages-option
When you create a new environment with virtualenv, among other things it creates a bash script venv/bin/activate (where venv is the folder you specified when you created the environment; libraries are located there as well, by the way). When you run it in your shell the environment variables become arranged so that pip installs new libraries in this environment's folder. See virtualenv docs for details, section "activate script".

virtualenvwrapper commands aren't working

tow-81-235:Projects pessimisticoptimism$ mkvirtualenv development
-bash: mkvirtualenv: command not found
tow-81-235:Projects pessimisticoptimism$ sudo pip install virtualenvwrapper
Password:
Requirement already satisfied (use --upgrade to upgrade): virtualenvwrapper in /Library/Python/2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Python/2.7/site-packages (from virtualenvwrapper)
Requirement already satisfied (use --upgrade to upgrade): virtualenv-clone in /Library/Python/2.7/site-packages (from virtualenvwrapper)
Cleaning up...
tow-81-235:Projects pessimisticoptimism$ mkvirtualenv development
-bash: mkvirtualenv: command not found
Why am I getting this error? I have virtualenv and virtualenvwrapper installed. I'd like to use mkvirtualenv and workon. I find it odd that virtualenv is working, but virtualenvwrapper isn't.
1st, ensure you're installing with sudo:
sudo pip install virtualenvwrapper
2nd, append the following lines to your .bashrc file (with nano ~/.bashrc):
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
3rd, reload your profile
source ~/.bashrc
Summary
I'm on a Mac and my answer is similar to #Ramces answer except it was with bash_profile. I just want to elaborate a little further for Mac users to be aware that there's a lot of different profiles including:
.bashrc
.bash_profile
.profile
Some files like .profile do not take precedence over .bash_profile (if it exists) and will then be ignored. If you successfully do the below steps and get a virtual env working, but then close out your terminal and 'workon command not found', then you need to setup for the correct profile. For a detailed answer, see here
Install Steps:
sudo pip install virtualenv
Installs virtualenv (allows you to separate your envrionments)
sudo pip install virtualenvwrapper
Installs virtualenvwrapper (allows you to use the 'workon' command)
nano ~/.bash_profile
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bash_profile
Reloads the profile. Going forward you only need step 5 (to create new environments) and step 6 (to run environments)
mkvirtualenv my_env
This creates your virtual environment (this example is with 'my_env')
workon my_env
This lets you work on a specific environment (this example is with 'my_env')
After installing the virtualenvwrapper package using pip, you also have to do some initialisation/set your preferences. See the introduction in the virtualenvwrapper docs.
Most relevant for finding the commands should be sourcing the virtualenvwrapper script into your shell. In the docs it is mentioned as
$ source /usr/local/bin/virtualenvwrapper.sh
You still have to adjust the path to your setup. My guess for your Mac would be:
$ source /Library/Python/2.7/site-packages/virtualenvwrapper.sh
Simple process
sudo apt-get install python-pip(if pip is not installed)
sudo pip install virtualenv
Create a dir to store your virtualenvs
mkdir ~/.virtualenvs
sudo pip install virtualenvwrapper
Run following command
export WORKON_HOME=~/.virtualenvs
Add virtualenvwrapper.sh to .bashrc
Add this line to the end of ~/.bashrc so that the virtualenvwrapper commands are loaded.
. /usr/local/bin/virtualenvwrapper.sh
you will find .bashrc.sh file in home directory by doing ctrl+h. if not then use find command to find .bashrc.sh "file ls -la ~/ | more"
Hit this command
source /usr/local/bin/virtualenvwrapper.sh
Hit this command
source ~/.bashrc
It sounds like you have multiple Python installations on your machine and virtualenvwrapper is not pointing to the right Python.
Find out which Python virtualenvwrapper is using. You get a hint where to look with which virtualenvwrapper.sh (In this case /usr/local/bin):
> /usr/local/bin/virtualenvwrapper.sh
If you don't get any return here make sure you use the correct pip when installing. The pip command might link to a different Python then you expect. Check your usr/local/bin directory for pip links (pip, pip2, pip2.7, pip3, pip3.5). It is easy to get system pip, pip2 and pip2.7 mixed up.
After you have found the Python location, add/update all paths in your .profile:
export WORKON_HOME=$HOME/venv
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2
source /usr/local/bin/virtualenvwrapper.sh
Finally reload your profile: source ~/.profile
I am on Mac OS X 10.9.2 and for me virtualenvwrapper.sh file was present in
/usr/local/bin/virtualenvwrapper.sh
So I simply copied this into ~/.profile file:
source /usr/local/bin/virtualenvwrapper.sh
And now my ~/.profile file looks something like this:
# MacPorts Installer addition on 2014-02-23_at_17:28:39: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.
source /usr/local/bin/virtualenvwrapper.sh
And now I am able to use virtualenvwrapper commands without any issue whatsoever
Users of the Anaconda (from Continuum) distribution of Python should note that
sudo pip install virtualenvwrapper
will be anaconda-aware. So if you
which python
that should give you an idea of where to point your virtualenv in your .bashrc and/or .profile configuration files.