0

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.

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

0

After crating a virtual environment with python 2.7 you can install your required packages

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.