Using Virtual environments in Sublime. Cant import modules - python

So I started programming 1 -2 months ago with python and I am using Sublime Text3. I tried to read about it but I don't seem to understand how to link my virtualenv to my python project.
When I create my virtualenv in the terminal (I am on macOS) and I open a new project in sublime I can't import my installed modules. How do I add the env to my python project?

I would suggest taking a look at PyCharm (instead of sublime). It's a great IDE for python.
I'm not sure how sublime works, but on PyCharm you can set an interpreter for a project and give it the path to your virtual env. Maybe there's something similar in sublime as well..?
My venv paths are usually something like wherever_you_store_your_project/project_name/venv/bin/python
EDIT
The venv in the path is how I name my virtual environments

Related

Activate existing venv on pycharm

I created venvs for couple diff projects using python3 -m venv my_project
Also use pyenv local version for managing the python version for each project as needed.
My projects folder looks like this:
my-project-one/
my-project-two/
my_project_one/
my_project_two/
The ones with hypens are the repo while the ones with underscores are the venv I create for them. (maybe stupid naming scheme but works for me).
I activate the venv with source ../my_project_one/bin/activate from within the one I want to work with.
It has been great since I was using VSCode. Now I want to use Pycharm and I can't figure out how to make venv work with Pycharm.
In the project interpreter section I can see the two versions under ~/.pyenv/versions/ but that doesn't detect the project dependent requirements I have installed on the venv.
Many articles mentions about adding the python executable unders Scripts folder in venv but there is no such folder in any of mine. (on MacOS)
What am I missing here? Did I create the venv incorrectly or following the instruction for the wrong implementation of virtual env because I really don't want to mess with a working setup just for Pycharm.
I hope it would at the very least detect the project level libraries and not show unresolved reference for them.

How to use virtual environnement with VS Code?

I used Sublime Text so far for my Django / Python developments but I want a more complete IDE including debug and console built and my choice was focused on VS COde
the problem is that I have a little trouble to start, or more exactly to launch a project
I developed a project in a virtual environment venv
I can not activate this virtual environment from VS Code Console
I read the VS Code doc about it and you have to select a Python interpreter Ctrl + Shift + P but I do not see my virtual environment
can someone explain to me and help me?
thanks in advance
This depends on what implementation of virtual environments you are using.
If you are using the standard venv or an implementation that's similar to it, such as virtualenv, just create the environment in a subfolder of your project, like this:
It will be auto-detected in this case. If you are using something like virtualenvwrapper or pipenv, the environments should be auto-detected as well.
However, if VSCode is failing to find your environment automatically, you may set the path to it manually. Inside your project root, create .vscode/settings.json with the following in it:
{"python.pythonPath": "path/to/python.exe"}

How to tell VScode, on my Mac running high Sierra, which version of python to use

I use VScode to write and run python. I use it with a Jupyter extension. I have 2 virtual environments created using Anaconda. One py27, for python 2.7.13 and the other, py36, for Python 3.6.5.
I have a simple code which I edited to only run in python2. When I run VScode, the virtual environment I run it in does not seem to matter. VScode seems to run but I can't figure out how to make it use py36 (python 3.6.5) or py27 (python 2.7.13).
By using different commands in the command palette, or double-clicking on phrases in the bottom border of VScode I can eventually get it to run which version I want. But I have not found a reproducible method.
I have tried about 100 times with no luck. I thought I found a method that would repeat but I tried it again and it didn't work.
I know this question is very vague but it would take pages to explain what I've tried. I have about 5 or 6 settings which I've tried a plethora of combinations. One of the commands I tried is to select a python interpreter. I have several options but none seem to have an effect. Also, in the bottom border of the VScode window, there is the name of a specific python interpreter. You can click on this to select a different python interpreter. As far as I can tell this doesn't do anything. Actually, it's a hindrance because a user may think they are actually using that interpreter.
I've tried both py27 and p36 but I get no difference. If I can get visual studio code to start out in py27, I can make it change to py36 and back to py27. But how to make it start in py27 seems random. My virtual environments are in my home directory, /User/myname/py27 for example along with the VScode app.
The documentation at https://code.visualstudio.com/docs/python/environments explains how to select your virtual environment. Without knowing where you installed the virtual environments the best I can tell you is you can manually specify what interpreter to use with the python.pythonPath setting or if you have both virtual environments in a directory outside of your workspace folder you can specify the common folder with the python.venvPath setting.

Copy Python App to a new Machine?

I have a Python app running on windows that has imports for the following packages:
requests
json
psycopg2
I copy the entire project (I used Pycharms to write the app and import the packages) to a new machine and expected it would work. The new machine is also windows and I'm trying to run my script from the command line (i.e. no Pycharm on the new machine).
Instead, I get an error saying "ModuleNotFoundError: No module named 'requests'"
If I look at the project, I have the directories:
venv
Lib
site-packages
requests
What am I missing/doing wrong?
You have a couple of options here but first the problem. You are exporting your code base to a new machine without the required Modules installed on that machine and/or within your Python project's environment. After you have python installed on your new machine, you need to be sure to point your PyCharm Project to the proper environment.
File > Default Preferences > Project Interpreter
The window that appears on the right will contain a drop down menu labeled Project Interpreter. If you click on the drop down, it should reveal a list of the available Python environments on your machine.
Based on your description of your site-packages directory I would assume you do not have your interpreter pointed the proper environment on your new machine. With that said, you would be better served creating a new virtual python environment on your machine and installing each relevant dependency within that environment.
Take a look at this post here for your first best option on re-creating your old python environment on your new machine.
EDIT: I apologize for not reading the question more thoroughly before answering the questions. If this is running on a Windows machine you will need to double check the environment path python is using. It is very easy to install python at a different PATH than the command line environment is checking on a Windows box. If for example your PATH is pointing to a different version of Python and PIP is installing packages somewhere else this issue can occur. Double check your System PATH for python and which version the command line is running.
On the new machine you must source venv/bin/activate so your path environment variables are set properly. In particular, which python should say venv/bin/python rather than /usr/bin/python. Also, take care to conda env update or pip install -r requirements.txt so you'll have suitable venv libraries on the new machine.

Please suggest best practices for using Virtualenv in Pycharm on Windows

I’m starting to learn Python using PyCharm IDE on windows. I have a specific question about where to start adding/ creating python files/ classes.
I installed Python 3.5 and installed PyCharm Community. Created a project first and then created a virtualenv. I can therefore switch between 'Python Interpreters'. But, when I open PyCharm project, I see a folder structure and I can’t make a decision where to start creating code files. I've added screenshot as reference.
Where to start adding creating code file packages? And, is this the right way of creating virtualenv?
I'd also appreciate if you could please suggest any related best practices, especially regarding using virtualenv in this scenario.
Create the virtualenv while creating the project. Click the gear icon to the right of the interpreter box at the new project dialog box. Choose "Create VirtuanEnv"
Then you can right click the project name and add a python file.
Typically when developing python code on windows using PyCharm, I create a subfolder in my User directory (C:/Users/Vasili/virtualenvs/), where I place all of my future virtual environments.
When creating a new project, I create a new virtualenv in that folder and set it as the interpreter for the project. PyCharm will use that venv every time you open that project. It will also activate it whenever you use the inline terminal within PyCharm so you can run executables such as nosetests, if you have it installed in the project virtualenv.
As for general project structure, python is fairly informal when it comes to this, but there are some standards that you could try to adhere to, such as:
creating a setup.py file with your project metadata and other data to allow uploading the project to pypi, or even building RPMS.
a requirements.txt file with packages that pip must install for the project to work
A tests folder with your unit tests, fixtures and so on
The actual package itself, named after your project, with a dunder init script (__init__.py) inside, to signify that it is a package
An open source license
A readme, or documentation that can generate HTML docs with sphinx
Anything else you think would help people use your software.
As you are using community edition, you will have some limitations of the features.
I would suggest you to create virtual env from terminal/command prompt.
This is the folder structure which I follow for development while using Pycharm:
C:\workspace\<virtual_env>
C:\workspace\<project_name>
Add the whole workspace folder to your Pycharm as project.
In order to create and activate virtual env in windows refer the following:
https://docs.djangoproject.com/en/1.10/howto/windows/

Categories

Resources