Python virtualenv --system-site-packages iPython - python

I am using EPD on OS X and have ipython installed. In my 'general' environment everything is functioning as expected. I installed virtualenv and virtualenvwrapper to generate a dev environment. I only want to install a small subset of 'new' modules (different versions), so I used:
mkvirtualenv development --python=epd --system-site-packages
Now what I would like to do is install ipython local to the virtual env. I believe that this is the preferred installation method as other techniques include adding code to ipython startup.
which pip & which python report that the virtual env files are being called. I can not install ipyhton though, because it already exists in my epd install.
Is it possible to create a virtualenv that uses the 'general' site-packages and then locally install ipython?

Sure, just tell pip to ignore the installed IPython:
pip install --ignore-installed ipython

Related

Python: When using venv, how do I include a package I installed outside of the venv? [duplicate]

I'm looking for a way to make a virtualenv which will contain just some libraries (which I chose) of the base python installation.
To be more concrete, I'm trying to import my matplotlib to virtualenv during the creation of virtualenv. It can't be installed efficiently with pip or easy_install since it misses some fortran compiler libs. The way I did it until now was to manually copy from:
/usr/lib/python2.7/dist-packages/ to virtualenv_name/lib/python2.7/dist-packages/
However this prevents the manully imported links to be registerd by yolk (which prints all currently available libs in virtualenv).
So, is there a way to do a selective variant of the
virtualenv --system-site-packages
Create the environment with virtualenv --system-site-packages . Then, activate the virtualenv and when you want things installed in the virtualenv rather than the system python, use pip install --ignore-installed or pip install -I . That way pip will install what you've requested locally even though a system-wide version exists. Your python interpreter will look first in the virtualenv's package directory, so those packages should shadow the global ones.
You can use the --system-site-packages and then "overinstall" the specific stuff for your virtualenv. That way, everything you install into your virtualenv will be taken from there, otherwise it will be taken from your system.
I am late to the game using python.3.8 and pip3 on Ubuntu 20.04.
The ONLY way to get rid of the annoying .local install for me was to set an environment variable (bash):
export PYTHONNOUSERSITE="true"
This does not need to be "true" anything will work. I would not go for a 0. ;-)
Install virtual env with
virtualenv --system-site-packages
and use pip install -U to install matplotlib

Issue with installing python modules

I am pretty new to python. Just been working through some online tutorials on udemy. I seem to have an issue with pip installing modules.
I've tried reinstalling them.
Upgrading my python version.
In VS I always just get module not found.
If I do it in the cmd prompt this is what I get below.
You are currently working on the base environment of your computer. For safety, you can first create a new virtual environment with
python3 -m venv -n new_env
So that you won't corrupt any default installations. Then, activate it with
source new_env/bin/activate
And update the pip and setuptools with
pip3 install --upgrade pip
pip3 install --upgrade setuptools
Finally, install numpy via
pip3 install numpy
However, I would recommend using Anaconda to build your virtual environment. When you install Anaconda and make sure it is included in the path of your terminal, all you need to type is
conda create -n new_env python=3.7 numpy
and it will automatically build the wheel for numpy. Here, "new_env" is just an example for a virtual environment name, and Python version 3.7 is also an example.
You can then, activate this conda environment by
conda activate new_env
To use this virtual environment, which you built either with "venv" or "conda", you should locate and activate this environment from the project interpreter settings in VS .
Finally, I would also recommend considering Pycharm IDE which can also help you with creating a virtual environment and installing packages in it.
It seems that you already have the packages installed. Using VS, please, be sure that you selected the correct Python interpreter (https://code.visualstudio.com/docs/python/environments)

install packages in Python2 with Python3

I am trying to install numpy, nltk, etc packages for Python 2 to run a code. But I have Python3 as well and the path variable is set to it. When I try to use any pip install command it shows the package is available in Python3's directory.
Also, I am using VSCode, so I did not add the path variable.
I suggest you use virtual environments. Because if you read about virtual environments, you will find that they are created for such cases.
To create virtual environments, you must do the following:
Make a note of the full file path to the custom version of Python you just installed.
virtualenv -p /home/username/opt/python-2.7.15/bin/python venv
In order to use this environment’s packages/resources in isolation, you need to “activate” it. To do this, just run the following:
source venv/bin/activate (Linux)
./venv/Scripts/activate.bat (Windows)
Notice how your prompt is now prefixed with the name of your environment (venv, in our case). This is the indicator that venv is currently active, which means the python executable will only use this environment’s packages and settings.
Now run the following:
(venv) $ which python
/Users/ashkan/python-virtual-environments/venv/bin/python (in my case)
now you have access to python2.7.
The best practice for this particular problem would be virtual environments.And for that matter Pipenv would be a good option.
Install Pipenv.
$ brew install pipenv (MacOs)
$ sudo apt install pipenv (Debian)
$ sudo dnf install pipenv (Fedora)
pip install pipenv (Windows)
Creating virtual env with Pipenv.
pipenv install --python 2.7 numpy
This command will install create a virtual environment and install python 2.7(which will be used as the main interpreter once you activate the environment) along with numpy in that environment. This will avoid the packages version conflicts too.
To activate the environment
pipenv shell
If you are working in the Vs Code workspace then you should set the interpreter path(python path) to the path of the virtual environment.
when we install anything using pip. it will install dependencies for default python version. so you can change the default python version using this link https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux
Hope this will solve your problem
After crating a virtual environment with python 2.7 you can install your required packages

No module named win32com

I've just installed Python for the first time and I'm trying to reference the win32com module however, whenever I try to import it I get the message "no module name win32com".
Any ideas?
the below plus add pywin32 in PyCharm setting work for me
python -m pip install pywin32
As it is not built into Python, you will need to install it.
pip install pywin
Since win32com is a Windows-specific package, this answer will be geared towards Windows users.
Option 1: Install locally with pipenv (recommended)
You can use a package manager like pipenv to manage your dependencies.
Ensure you have pipenv installed (pip install pipenv).
In your project directory, run pipenv install pypiwin32 to install the package.
Now you can run your code using commands like the following pipenv run main.py
Example main.py code:
import win32com
print(win32com)
Option 2: Install locally with venv (recommended)
If pipenv isn't your thing, you can use the built-in virtual environments.
From your project directory, run python -m venv venv to setup you virtual environment.
Run venv\Scripts\activate.bat from your project directory whenever you want to use this virtual environment (you will see (venv) added to your shell prompt to know it's activated).
Run pip install pypiwin32 from your active virtual environment to install the package.
You can run your code like python main.py so long as the virtual environment is active.
Option 3: Install globally (typically not recommended)
This is not typically recommended, but included anyway.
Using pip install pypiwin32 you can install the package globally.
Then you can run your code with just python main.py.
This will work as well
python -m pip install pywin32
You should try using pip this way:
pip install pypiwin32
It is pypiwin32 which should work.
When working with python projects its always a good idea to create a so called virtual environment, this way your modules will be more organized and reduces the import errors.
for example lets assume that you have a script.py which imports multiple modules including pypiwin32.
here are the steps to solve your problem:
1. depending on you operating system you need to download and install virtualenv package, in debian its as simple as sudo apt install virtualenv .
2. after installing 'virtualenv' package go to your project/script folder and create a virtualenv folder with virtualenv venv it creates a folder named venv in that directory.
3. activate your virtualenv source /path/to/venv/bin/activate if your already in the directory where venv exists just issue source venv/bin/activate
4. after activating your venv install you project dependencies pip install pypiwin32 or pip install pywin
5. run your script, it wont throw that error again :)

Call correct pip in pyvenv environment python3.4

I installed a new pyvenv environment with the following commands:
python3.4 -m venv env
source env/bin/activate
However, when I call which pip, I get the following: /usr/bin/pip. Apparently, the system wide pip installation is still used. If I look at the pyvenv documentation, it states the following:
Changed in version 3.4: Installs pip by default, added the
--without-pip and --copies options
And this is correct, when trying to install pip in my activated environment, I get the following:
Requirement already up-to-date: pip in ./env/local/lib/python3.4/dist-packages
How do I make sure that when I call pip in my activated environment, pyvenv pip is called?
Looks like you (and I in my previous answer) were seeing the effects of a bug. Everything seems to be working in the newer versions of pyvenv-3.4.
$ pwd
~/test
$ pyvenv-3.4 myenv
$ source myenv/bin/activate
(myenv)$ which pip
~/test/myenv/bin/pip
I was having a similar issue, I asked my webhost (WebFaction) and got an excellent response. The Understanding pyvenv and pip in virtual environments with python 3.4 and above page is summarized below.
It seems that you don't have to worry about which pip your virtual environment is using. As long as your virtual environment is active, any packages will be installed in it, regardless of which pip is used.
One thing about Python 3.4 virtual environments is that if the package does not support Python's new "wheel" package format, the package will not install inside the virtual environment directory. It will still maintain a connection with the virtual environment however.
Because of this strangeness, I opted to just use virtualenv instead of pyvenv-3.4 and everything worked as expected.
The issue has been fixed on Ubuntu 14.10.
And if you installed python 3.4 by the following command on Centos6.
yum install http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/python34u-3.4.2-1.ius.centos6.x86_64.rpm
You should reopen a new terminal (or relogin your system).
It works for me. Good luck.

Categories